在我的AVSpeechSynthesizer应用程序中,我使用了TTS的AVSpeechSynthesizer。但是没有调用didFinishSpeechUtterance方法。
这是我在.h中的代码:
#import <AVFoundation/AVFoundation.h>
@interface EmailViewController : UIViewController <UITextFieldDelegate, AVSpeechSynthesizerDelegate>
@property (nonatomic, strong) AVSpeechSynthesizer* talker;这是在.m中
- (void) startTalking {
NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
AVSpeechUtterance* utter = [[AVSpeechUtterance alloc] initWithString:hi];
utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utter setRate:0.2f];
if (!self.talker) {
self.talker = [AVSpeechSynthesizer new];
}
self.talker.delegate = self;
[self.talker speakUtterance:utter];
}
- (void)stopSpeech
{
AVSpeechSynthesizer *talked = [[AVSpeechSynthesizer alloc] init];
if([talked isSpeaking]) {
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[talked speakUtterance:utterance];
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utteranc
{
if (speaknumber < (length-1)) {
speaknumber += 1;
self.talker.delegate = self;
NSString *hi = [NSString stringWithFormat:@"%@", humanLanguageArray[speaknumber]];
AVSpeechUtterance *utter = [[AVSpeechUtterance alloc] initWithString:hi];
utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utter setRate:0.2f];
if (!self.talker) {
self.talker = [AVSpeechSynthesizer new];
}
[self.talker speakUtterance:utter];
} else if (speaknumber >= length) {
}
}如果我打电话给stopSpeech,它就会不停地说话,直到speaknumber >= length。有人知道怎么解决这个问题吗?
发布于 2014-07-10 23:52:28
为什么要再次在AVSpeechSynthesizer中实例化stopSpeech?
- (void)stopSpeech
{
AVSpeechSynthesizer *talked = self.talker;
if([talked isSpeaking]) {
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
[talked speakUtterance:utterance];
[talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}说话的例子是self.talker,不是吗?
https://stackoverflow.com/questions/24687719
复制相似问题