AVSpeechSynthesizer委托方法didStartSpeechUtterance没有被调用

时间:2014-08-10 23:07:26

标签: ios7 delegates avspeechsynthesizer

我遇到麻烦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?

感谢。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

@interface ViewController : UIViewController <AVSpeechSynthesizerDelegate>

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

希望它有所帮助。