我希望在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
但我无法锻炼如何在我的应用程序中实现这一点。
答案 0 :(得分:1)
快速查看AVSpeechSynthesizer
的文档会发现它有delegate
属性。
您应该设置delegate
并实施AVSpeechSynthesizerDelegate
协议,以便您可以收到有关语音合成器事件的通知。
等事件
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
didFinishSpeechUtterance:(AVSpeechUtterance *)utterance;
和
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
didStartSpeechUtterance:(AVSpeechUtterance *)utterance;
对你来说最有意思,考虑到你想知道它何时开始和停止。还有一些事件可以被取消,暂停和继续,您可能还需要实现这些事件来隐藏和显示您的用户界面。