我想在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。

09-11 19:51