我正在开发一个应用程序,该应用程序使用AVSpeech
读取UITextField
中的文本,当它读取特定单词时,我试图暂停阅读,例如它读取此This is awesome.
,我希望它在读取is
时暂停阅读2秒,然后完成阅读。下面是我读取字符串的代码。
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"This is awesome"];
utterance.rate = 0.15;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-us"];
[synth speakUtterance:utterance];
谢谢
在出现"is"时将语句字符串分开,并将组件放入数组中。按顺序读取数组中的每个字符串,中间有一个sleep(2)
。记住,在主线程上睡觉会禁用你的UI交互。
-(void)pauseSpeechReading
{
if([synthesize isSpeaking]) {
NSLog(@"Reading paused");
[synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[synthesize speakUtterance:utterance];
[synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
-(void)resumeSpeechReading
{
NSLog(@"Reading resumed");
[synthesize continueSpeaking];
}
这是NSHipster上关于iOS版TTS的好文章- http://nshipster.com/avspeechsynthesizer/
请阅读委托部分的文章-你找到一种方式来显示当前说出来的单词。然后使用synthesize pauseSpeakingAtBoundary暂停2秒,然后恢复TTS。
完整的示例项目在GitHub上:https://github.com/mattt/AVSpeechSynthesizer-Example。您可以找到其他一些使用TTS的有用技术。
拆分为两个话语,并设置第二个为。preutterancedelay = 2.0