2

我创建了一个AVSpeechSynthesizer并开始播放一个AVSpeechUtterance. 这工作正常。如果用户按下按钮,我会暂停合成器。这也有效。但是,当我尝试使用该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];
    }

}

4

4 回答 4

1

您的原始代码是:

if (synthesizer_.isSpeaking) {

试试这个:

if (![synthesizer_ isPaused])

原因:

来自文档: speak 一个布尔值,指示合成器是否在说话。(只读)

@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking

讨论YES如果合成器正在讲话或有话语排队等待讲话,则返回,即使它当前已暂停。NO如果合成器已经完成了其队列中的所有话语,或者尚未给它一个话语来说话,则返回。

可用性 适用于 iOS 7.0 及更高版本。宣布于 AVSpeechSynthesis.h

因此,如果您使用此属性,并且语音暂停,则此属性可以为真,您的语音继续代码将不会运行。

于 2013-11-22T17:48:39.167 回答
1

只需从 isPaused 开始,它就可以正常工作:

注意:使用 AVSpeechBoundaryImmediate 而不是 AVSpeechBoundaryWord 以获得更好的结果。

- (IBAction)pauseButtonPressed:(id)sender {
    if (synthesizer_.isPaused) {
        [synthesizer_ continueSpeaking];
    }
    else{
        [synthesizer_ pauseSpeakingAtBoundary: AVSpeechBoundaryImmediate];
    }
}
于 2015-07-11T16:05:29.897 回答
0

尝试使用以下代码

-(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];
}
于 2014-12-08T04:28:26.760 回答
0

我在 GitHub 上发现了这个非常好用且易于使用的 AVSpeechSynthesizer 库。

它使用 NSMutableArray 正确管理多个话语。以及正确实施暂停、停止和继续。

https://github.com/quentinhayot/QHSpeechSynthesizerQueue

我更新了 -(void)next 函数以纠正 isSpeaking 时出现的问题:

-(void)next{
    if (_play && [_queue count] > 0 && ![_synthesizer isSpeaking]){
        AVSpeechUtterance *utterance = [_queue firstObject];
        [_queue removeObjectAtIndex:0];
        [_synthesizer speakUtterance:utterance];
    }

    //added this to fix issue when already speaking
    else if ([_synthesizer isSpeaking]) {
        NSLog(@"AVSpeech isSpeaking");
        [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(tmrNextDelay_Method:) userInfo:nil repeats:NO];
    }
}

-(void)tmrNextDelay_Method:(NSTimer *)timer{
    [self next];
}
于 2018-09-06T08:24:51.123 回答