AVSpeechSynthesizer - If AVSpeechSynthesizer is Speaking & if has stopped speaking

百般思念 提交于 2019-12-12 02:01:32

问题


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.


回答1:


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

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

Events such as

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

and

- (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.



来源:https://stackoverflow.com/questions/28326487/avspeechsynthesizer-if-avspeechsynthesizer-is-speaking-if-has-stopped-speaki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!