swift - 如何突出显示每个段落并使用 speechSynthesizer 滚动 textView

标签 swift speech-to-text speechsynthesizer

我想做的是按下朗读按钮,这将突出显示第一段,然后朗读该段落,然后将下一段滚动到 textView 的中心并突出显示该段落并朗读它等等。 我必须添加什么代码才能做到这一点?

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>]()

开始时

   let text = textViewOutlet.text!
   paragraphs = splitOnParagraphs(text)
   currentParagraphIndex = 0
   highlightParagraph(paragraphs[currentParagraphIndex])
   let utterance = AVSpeechUtterance(text.substringWithRange(paragraphs[currentParagraphIndex]))
   utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
   synthesizer.speak(utterance)

另见 Split paragraphs into Sentences

所以你开始说第一段,而不仅仅是文字。

然后,当段落结束时,在委托(delegate)中你开始说下一段:

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)
     }
}

关于swift - 如何突出显示每个段落并使用 speechSynthesizer 滚动 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49537860/

相关文章:

ios - 处理具有相同签名但参数不同的多个函数的最有效方法

xcode - 1 个 UIImagePickerController 和 2 个按钮

ios - 在 Swift 中将数据传递给另一个 ViewController

java - 如何设置语音识别服务器?

Azure Batch 转录 : Error when downloading the recording URI. 状态代码:冲突(下载失败)

c# - 是否有任何日语 TTS 语音可与 C# SpeechSynthesizer 一起使用

c# - C# 中的 SpeechSynthesizer 创建具有 22kHz 的 wav...需要为 16kHz

objective-c - 扩展所有 Swift 对象

android - Flutter语音识别应用程序-_platformCallHandler调用speech.onError 2

javascript - 使用 Web Speech API 的 SpeechSynthesis 接口(interface)改善发音