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

  • 在oneViewController上,有一个使用Storyboard调用TwoViewController的按钮-可以正常工作
  • 在TwoViewController上有一个按钮,可以使用按钮(“监听”按钮)读取一些文本-可以正常工作
  • 在TwoViewController上有Back按钮,可将您带回到oneViewController。

  • 问题:

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

    在TwoViewController上的代码:

    AVSpeechSynthesizer *合成器;
    -(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];
    
    }
    

    注意:如果单击一次“监听”按钮,然后单击“返回”按钮,则不会发生此问题。仅当多次单击“侦听”按钮然后单击“后退”按钮时,才会发生问题。

    最佳答案

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

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

    关于ios - AVSpeechSynthesizer-使用AVSpeechBoundaryImmediate时不会停止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25457297/

    10-12 03:44