AVSpeechSynthesizer - 使用AVSpeechBoundaryImmediate时不会停止

时间:2014-08-23 00:27:42

标签: ios text-to-speech avspeechsynthesizer

我有两个视图控制器。一和二。

  1. 在oneViewController上,有一个使用Storyboard调用TwoViewController的按钮 - 工作正常
  2. 在TwoViewController上有一个按钮,使用按钮(Listen Button)读取一些文本 - 工作正常
  3. TwoViewController上有Back按钮,可以将你带回oneViewController。
  4. 问题:

    如果单击“监听”按钮两次并按下“返回”按钮,则视图控制器从“二”变为“一”,但语音仍在后台进行。有人可以帮助阻止演讲吗?

    TwoViewController上的代码:

    AVSpeechSynthesizer * synth;

    -(IBAction) Back:(id)sender
    {
        [_synth stopSpeakingAtBoundary: AVSpeechBoundaryImmediate]; --not working
        [self dismissViewControllerAnimated:YES completion:nil]; - working fine.
    }
    
    -(void) viewDidDisappear:(BOOL)animated
    {
        [_synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; --not working
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    -(IBAction)listenButton:(id)sender
    {
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"I am running a test."];
        [utterance setRate:0.18f];
        _synth=[[AVSpeechSynthesizer alloc] init];
        [_synth speakUtterance:utterance];
    
    }
    

    注意:如果单击“监听”按钮一次,然后单击“后退”按钮,则不会发生此问题。只有在多次单击“监听”按钮并单击“后退”按钮时才会出现问题。

1 个答案:

答案 0 :(得分:1)

尝试暂停,恢复和停止选项

-(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];
}