首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVSpeechSynthesizer不停止

AVSpeechSynthesizer不停止
EN

Stack Overflow用户
提问于 2014-07-10 23:04:41
回答 1查看 3.7K关注 0票数 4

在我的AVSpeechSynthesizer应用程序中,我使用了TTS的AVSpeechSynthesizer。但是没有调用didFinishSpeechUtterance方法。

这是我在.h中的代码:

代码语言:javascript
复制
#import <AVFoundation/AVFoundation.h>

@interface EmailViewController : UIViewController <UITextFieldDelegate, AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer* talker;

这是在.m中

代码语言:javascript
复制
- (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。有人知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-10 23:52:28

为什么要再次在AVSpeechSynthesizer中实例化stopSpeech?

代码语言:javascript
复制
- (void)stopSpeech
{
    AVSpeechSynthesizer *talked = self.talker;
    if([talked isSpeaking]) {
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
        [talked speakUtterance:utterance];
        [talked stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}

说话的例子是self.talker,不是吗?

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24687719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档