【问题标题】:AVSpeechSynthesizer continueSpeaking not workingAVSpeechSynthesizer continueSpeaking 不工作
【发布时间】:2022-03-04 15:06:04
【问题描述】:

我创建了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];
    }

}

【问题讨论】:

  • 我有一个类似的问题,[synthesizer_ pauseSpeakingAtBoundary:AVSpeechBoundaryWord]doesn't 暂停发言。相反,我会收到多个 didPauseSpeechUtterance / didContinueSpeechUtterance 事件,直到话语结束。使用pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate 似乎效果更好。
  • 我删除了代码中对 AVAudio 项目的所有其他引用,然后我遇到了与您相同的事情,重复了 didPauseSpeechUtterance/didContinueSpeechUtterance 事件。

标签: ios objective-c text-to-speech


【解决方案1】:

你原来的代码是:

if (synthesizer_.isSpeaking) {

试试这个:

if (![synthesizer_ isPaused])

原因:

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

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

讨论 如果合成器正在讲话或有话语排队等待讲话,则返回 YES,即使它当前已暂停。如果合成器已经完成了其队列中的所有话语,或者还没有被赋予话语权,则返回 NO

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

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

【讨论】:

    【解决方案2】:

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

    注意:使用 AVSpeechBoundaryImmediate 代替 AVSpeechBoundaryWord 以获得更好的结果。

    - (IBAction)pauseButtonPressed:(id)sender {
        if (synthesizer_.isPaused) {
            [synthesizer_ continueSpeaking];
        }
        else{
            [synthesizer_ pauseSpeakingAtBoundary: AVSpeechBoundaryImmediate];
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试使用跟随代码

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

      【讨论】:

        【解决方案4】:

        我在 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];
        }
        

        【讨论】:

          猜你喜欢
          • 2019-06-28
          • 2022-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多