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

时间:2018-03-28 14:56:21

标签: swift speech-to-text speechsynthesizer

我要做的是按下发言按钮,突出显示第一段,然后说出那段,然后将下一段滚动到textView的中心,然后突出显示那段并说出来就这样拥有。 我必须添加什么代码才能执行此操作?

#include <iostream>

using namespace std;

int main(){

    string arr = " ";
    int x;
    cout << "Enter your input";
    cin >> x;
    for (int i =0; i< arr.length(); i++ ){
        if (arr[i] == x){
            break;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:-1)

这样的事情:

定义属性

   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

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

然后,当段落结束时,在代表中你开始说下一段:

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