【问题标题】:Swift 3, highlight word when spoken in AVSpeechSynthesizer?Swift 3,在 AVSpeechSynthesizer 中说话时突出显示单词?
【发布时间】:2017-09-08 14:14:20
【问题描述】:

试图在 AVSpeechSynthesizer 操作期间突出显示一个单词。朗读文本,但单词未突出显示。该文本包含在 func speechSynthesizer() 中的文本视图 infoTxt 中。

仍然需要弄清楚如何在说话后取消突出显示。

@IBAction func ReadText(_ sender: UIButton) {

    if !self.synth.isSpeaking  {

        let textParagraphs = self.infoTxt.text.components(separatedBy: "\n")
        self.totalUtterance = textParagraphs.count

        for pieceOfText in textParagraphs {

            let speechUtterance = AVSpeechUtterance(string: pieceOfText)

            let voice = AVSpeechSynthesisVoice(language: "en GB")
            speechUtterance.voice = voice
            _ = AVSpeechSynthesisVoice.speechVoices()
            self.synth.speak(speechUtterance)
        }


        func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
            let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
            mutableAttributedString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: characterRange)

            infoTxt.attributedText = mutableAttributedString
        }
    }else {
        self.synth.continueSpeaking()
    }

    myUtterance = AVSpeechUtterance(string: infoTxt.text)
    myUtterance.rate = 0.1
    _ = AVSpeechSynthesisVoice()
    synth.speak(myUtterance)
    _ = AVSpeechSynthesisVoice(language: "Tom")
}

@IBAction func PauseText(_ sender: Any) {
    self.synth.pauseSpeaking(at: AVSpeechBoundary.word)
}

@IBAction func StopText(_ sender: Any) {
    self.synth.stopSpeaking(at: AVSpeechBoundary.immediate)
}

【问题讨论】:

  • 你设置委托了吗?
  • Yesclass ViewController: UIViewController, AVSpeechSynthesizerDelegate
  • 不,我的意思是my.synth.delegate = self
  • 嗨,笑话人,我是这么认为的: let synth = AVSpeechSynthesizer() var myUtterance = AVSpeechUtterance(string: "")

标签: ios swift text highlighting


【解决方案1】:

遵循@jokeman 明确的评论,但包括一个 Swift 3 语法示例

为视图添加标签并使其可用

@IBOutlet weak var lblExerciseMessage: UILabel!

例如在 viewDidLoad/viewWillAppear 中初始化语音引擎

let voice = AVSpeechSynthesisVoice.speechVoices().first(where: { $0.name == "Samantha" })

let synth = AVSpeechSynthesizer()

synth.delegate = self as? AVSpeechSynthesizerDelegate //Set the delegate. No need to type it; just for highlighting

let utter = AVSpeechUtterance(string: "Dear Coder, please use google to find the answers. smiley ")
 utter.voice = voice

synth.speak(utter)

然后由委托函数调用:speechSynthesizer 并填充标签参考

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {

    let mutableAttributedString = NSMutableAttributedString(string: utterance.speechString)
    mutableAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range:  NSMakeRange(0, (utterance.speechString as! NSString).length))
    mutableAttributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: characterRange)
    lblExerciseMessage.attributedText = mutableAttributedString
    print (mutableAttributedString) //debug message
}

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多