2

我在使用 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?

谢谢。

4

2 回答 2

1

回答有点晚了,见谅。为了解决这个问题,还要输入:

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-02-05T09:54:27.323 回答
0

另外不要忘记在 .h 文件中添加对 AVSpeechSynthesizerDelegate 协议的引用,例如:

@interface ViewController : UIViewController <AVSpeechSynthesizerDelegate>

然后你可以把你的 synthesizer.delegate = self; 仅在您的 ViewDidLoad 方法中。

希望能帮助到你。

于 2015-04-02T07:48:39.093 回答