0

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

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

4

1 回答 1

1

快速浏览一下文档AVSpeechSynthesizer会发现它有一个delegate属性。

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

事件如

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

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

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

于 2015-02-04T16:36:00.870 回答