【问题标题】:AVSpeechSynthesizer - If AVSpeechSynthesizer is Speaking & if has stopped speakingAVSpeechSynthesizer - 如果 AVSpeechSynthesizer 正在说话并且已经停止说话
【发布时间】:2015-02-05 00:42:44
【问题描述】:

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

但我无法确定如何在我的应用中实现这一点。

【问题讨论】:

    标签: ios xcode if-statement text-to-speech avspeechsynthesizer


    【解决方案1】:

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

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

    事件如

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

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

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

    【讨论】:

    • 非常感谢您的回复,使用我当前的 -(void)speakText,如何调用 DidFinishSpeechUtterance?我已经有委托 @interface ViewController : UIViewController {
    • 听起来您需要了解一下委托模式的工作原理。您设置一个委托对象,如果该对象实现了协议,则该对象将接收对委托方法的调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多