我在AVSpeechSynthesizer和话语方面遇到了问题(ios 7.1.1)
AVSpeechSynthesizer在处理程序类中初始化,并将委托设置为self:
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;我在委托中实现了以下方法:
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"Finish");
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
NSLog(@"Start");
}我的语音是通过这个方法调用的:
- (void)speak:(NSString *)spokenWord{
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:spokenWord];
[self.synthesizer speakUtterance:utterance];
}然后我重复调用该方法,例如
[AVhandler speak:_word];
[AVhandler speak:_word];
[AVhandler speak:_word];我期望在日志中看到:开始、结束等
但我看到的是:开始结束结束
为什么没有调用didStartSpeechUtterance?
谢谢。
发布于 2015-02-05 17:54:27
回答有点晚了,抱歉。要解决此问题,还应放入:
synthesizer.delegate = self;在-(void)中,如下所示
- (void)speak:(NSString *)spokenWord{
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:spokenWord];
[self.synthesizer speakUtterance:utterance];
synthesizer.delegate = self;
}在didFinish中,如下所示:
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
NSLog(@"Finish");
synthesizer.delegate = self;
}发布于 2015-04-02 15:48:39
另外,不要忘记在.h文件中添加对AVSpeechSynthesizerDelegate协议的引用,例如:
@interface ViewController : UIViewController <AVSpeechSynthesizerDelegate>然后,您可以只将synthesizer.delegate = self;放在ViewDidLoad方法中。
希望能有所帮助。
https://stackoverflow.com/questions/25233866
复制相似问题