Swift 如何使用speechSynthesizer突出显示每个段落并滚动文本视图
我想做的是按下speak按钮,突出显示第一段,然后说出该段,然后将下一段滚动到textView的中心,突出显示该段,然后说出该段。 要做到这一点,我需要添加什么代码Swift 如何使用speechSynthesizer突出显示每个段落并滚动文本视图,swift,speech-to-text,speechsynthesizer,Swift,Speech To Text,Speechsynthesizer,我想做的是按下speak按钮,突出显示第一段,然后说出该段,然后将下一段滚动到textView的中心,突出显示该段,然后说出该段。 要做到这一点,我需要添加什么代码 import UIKit import AVFoundation class TestViewController: UIViewController, AVSpeechSynthesizerDelegate { let synthesizer = AVSpeechSynthesizer() @IBOutlet weak
import UIKit
import AVFoundation
class TestViewController: UIViewController, AVSpeechSynthesizerDelegate {
let synthesizer = AVSpeechSynthesizer()
@IBOutlet weak var textViewOutlet: UITextView!
@IBAction func pauseSpeech(_ sender: Any) {
synthesizer.pauseSpeaking(at: AVSpeechBoundary.word)
}
@IBAction func stopSpeech(_ sender: Any) {
synthesizer.stopSpeaking(at: .word)
}
@IBAction func cancelButton(_ sender: Any) {
synthesizer.continueSpeaking()
}
@IBAction func speak(_ sender: AnyObject) {
let string = textViewOutlet.text!
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
override func viewDidLoad() {
super.viewDidLoad()
synthesizer.delegate = self
textViewOutlet.font = .systemFont(ofSize: 20)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
mutableAttributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, (utterance.speechString as NSString).length))
mutableAttributedString.addAttribute(.foregroundColor, value: UIColor.red, range: characterRange)
textViewOutlet.attributedText = mutableAttributedString
textViewOutlet.font = .systemFont(ofSize: 20)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
textViewOutlet.attributedText = NSAttributedString(string: utterance.speechString)
textViewOutlet.font = .systemFont(ofSize: 20)
}
}
大概是这样的:
定义属性
let currentParagraphIndex = 0
let paragraphs = [Range<String.Index>]()
另见
所以你开始讲第一段,而不仅仅是文本
然后,当段落结束时,在“学员”中开始讲下一段:
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
currentParagraphIndex = currentParagraphIndex + 1
if (currentParagraphIndex < paragraphs.count) {
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
highlightParagraph(currentParagraphIndex)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
}
func speechsynthesis(u合成器:avspeechsynthesis,didfish发音:avspeechoutance){
currentParagraphIndex=currentParagraphIndex+1
if(当前段落索引<段落计数){
let outrance=avspeechoutrance(text.substringWithRange(段落[currentParagraphIndex]))
高亮段落(当前段落索引)
outrance.voice=AVSpeechSynthesisVoice(语言:“en-GB”)
合成器。说话(说话)
}
}
可能的副本您必须使用AVSpeechSynthesizerDelegateI调用viewDidLoad中的代理。如何通过代理和滚动来逐段检测。我不知道如何用这个答案来解决问题,所以我编辑了我的问题。谢谢你的指导,我已经了解了更多。
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
currentParagraphIndex = currentParagraphIndex + 1
if (currentParagraphIndex < paragraphs.count) {
let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
highlightParagraph(currentParagraphIndex)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
synthesizer.speak(utterance)
}
}