AVSpeechSynthesizer - 如果 AVSpeechSynthesizer 正在说话并且已经停止说话

AVSpeechSynthesizer - If AVSpeechSynthesizer is Speaking & if has stopped speaking

我想在 AVSpeechSynthesizer 说话时在我的应用程序上显示一个视图,并在它停止说话时让该视图消失。

-(void)speakText {
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    float speechSpeed = 0.12;
    AVSpeechUtterance *synUtt = [[AVSpeechUtterance alloc] initWithString:textString];
    [synUtt setRate:speechSpeed];
    [synUtt setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:selectedVoice]];
    [synthesizer speakUtterance:synUtt];


//BELOW TO APPEAR AND AND DISAPPEAR

        [UIButton beginAnimations:nil context:nil];
        [UIButton setAnimationDuration:0.5];
        [UIButton setAnimationDelay:0.0];
        [UIButton setAnimationCurve:UIViewAnimationCurveEaseOut];
        _speakingScrollView.frame = CGRectMake(236, 675, _speakingScrollView.frame.size.width, _speakingScrollView.frame.size.height);
        [self.view bringSubviewToFront:_speakingScrollView];
        [UIView commitAnimations];

}

我似乎不知道该怎么做? 我看到苹果文档建议

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

但我不知道如何将其应用到我的应用程序中。

快速查看 AVSpeechSynthesizer 的文档会发现它有一个 delegate 属性.

您应该设置 delegate 并实施 AVSpeechSynthesizerDelegate 协议,以便您可以收到语音合成器事件的通知。

事件如

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
 didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
  didStartSpeechUtterance:(AVSpeechUtterance *)utterance;

考虑到您想知道它何时开始和停止,这对您来说是最有趣的。还有用于取消、暂停和继续的事件,您可能还想实现这些事件来隐藏和显示您的 UI。