Ios AVSpeechSynthesizer继续讲话不工作
我已经创建了一个Ios AVSpeechSynthesizer继续讲话不工作,ios,objective-c,text-to-speech,Ios,Objective C,Text To Speech,我已经创建了一个avspeechsynthesis并开始播放avspeechoutrance。这个很好用。如果用户按下按钮,我会暂停合成器。这也行得通。但是,当我尝试用continueSpeaking方法重新启动合成器时,什么都没有发生。如果我检查isSpeaking属性,它仍然是NO。如何让音频从停止的位置重新开始播放 AVSpeechSynthesizer *synthesizer_; synthesizer_ = [[AVSpeechSynthesizer alloc] init]; s
avspeechsynthesis
并开始播放avspeechoutrance
。这个很好用。如果用户按下按钮,我会暂停合成器。这也行得通。但是,当我尝试用continueSpeaking
方法重新启动合成器时,什么都没有发生。如果我检查isSpeaking
属性,它仍然是NO
。如何让音频从停止的位置重新开始播放
AVSpeechSynthesizer *synthesizer_;
synthesizer_ = [[AVSpeechSynthesizer alloc] init];
synthesizer_.delegate = self;
- (void)textToSpeech{
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:itemText];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:localeCode];
utterance.rate = UTTERANCE_RATE;
utterance.preUtteranceDelay = itemDelayTimeInterval;
[synthesizer_ speakUtterance:utterance];
}
- (IBAction)pauseButtonPressed:(id)sender {
if (synthesizer_.isSpeaking) {
[synthesizer_ pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
}
else{
[synthesizer_ continueSpeaking];
}
}您的原始代码是:
if (synthesizer_.isSpeaking) {
请尝试以下方法:
if (![synthesizer_ isPaused])
原因:
从文档中:
讲话
指示合成器是否正在讲话的布尔值。(只读)
讨论
如果合成器正在讲话或语音排队等待讲话,则返回YES
,即使合成器当前已暂停。如果合成器已说出其队列中的所有话语,或者尚未给出要说话的话语,则返回NO
可用性
在iOS 7.0及更高版本中提供。
声明于
AVSpeechSynthesis.h
因此,如果您使用此属性,并且语音被暂停,则此属性可能为true,并且您的语音继续代码将不会运行。尝试使用后续代码
-(void)stopSpeechReading
{
if([synthesize isSpeaking]) {
NSLog(@"Reading has been stopped");
[synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[synthesize speakUtterance:utterance];
[synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
-(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];
}
只需从isPaused开始,它就可以正常工作: 注意:为了获得更好的结果,请使用AVSpeechBoundaryImmediate而不是AVSpeechBoundaryWord
- (IBAction)pauseButtonPressed:(id)sender {
if (synthesizer_.isPaused) {
[synthesizer_ continueSpeaking];
}
else{
[synthesizer_ pauseSpeakingAtBoundary: AVSpeechBoundaryImmediate];
}
}
我在GitHub上发现了这个非常好用的AVSpeechSynthesizer库 它使用NSMutableArray正确管理多个语句。 以及正确执行暂停、停止和继续
我有一个类似的问题,
[synthesizer\uuu暂停讲话边界:AVSpeechBoundaryWord]
不暂停讲话。相反,我会得到多个didpausespeechutrance
/didcontinuespeechutrance
事件,直到发言结束。使用pausespeechatboundary:AVSpeechBoundaryImmediate
似乎效果更好。我删除了代码中对AVAudio项的所有其他引用,然后我经历了与您相同的事情,重复了didPauseSpeechUtterance
/didContinueSpeechUtterance
事件。
- (IBAction)pauseButtonPressed:(id)sender {
if (synthesizer_.isPaused) {
[synthesizer_ continueSpeaking];
}
else{
[synthesizer_ pauseSpeakingAtBoundary: AVSpeechBoundaryImmediate];
}
}