AVSpeechSynthesizer - 如果AVSpeechSynthesizer说话&如果已经停止说话 [英] AVSpeechSynthesizer - If AVSpeechSynthesizer is Speaking & if has stopped speaking

查看:131
本文介绍了AVSpeechSynthesizer - 如果AVSpeechSynthesizer说话&如果已经停止说话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在AVSpeechSynthesizer讲话时在我的应用上显示一个视图,并且当视图停止讲话时视图会消失。

I want to display a view on my app whilst AVSpeechSynthesizer is speaking, and for the view to disappear when it has stopped speaking.

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

}

我似乎无法理解如何去对这个?
我见过苹果文档建议

I can't seem to work out out how to go about this? I've seen the apple documentation suggests

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

但我无法锻炼如何在我的应用程序中实现这一点。

But I can't workout how to implement this into my app.

推荐答案

快速查看 AVSpeechSynthesizer 的文档,发现它有一个委托 property。

A quick look at the docs for AVSpeechSynthesizer reveals that it has a delegate property.

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

You should set the delegate and implement the AVSpeechSynthesizerDelegate protocol so that you can be notified of events for the speech synthesizer.

事件如

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

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

对你来说最有意思,因为你想知道它何时开始和停止。还有一些事件可以被取消,暂停和继续,您可能还需要实现这些事件来隐藏和显示您的用户界面。

would be most interesting to you, considering you want to know when it starts and stops. There are also events for being canceled, paused, and continued, which you will probably also want to implement to hide and show your UI.

这篇关于AVSpeechSynthesizer - 如果AVSpeechSynthesizer说话&如果已经停止说话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆