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

AVSpeechSynthesizer continueSpeaking不工作
EN

Stack Overflow用户
提问于 2013-10-15 03:21:52
回答 4查看 2.2K关注 0票数 2

我已经创建了一个AVSpeechSynthesizer并开始播放AVSpeechUtterance。这可以很好地工作。如果用户按下按钮,我会暂停合成器。这也是可行的。但是,当我尝试使用continueSpeaking方法重新启动合成器时,什么也没有发生。如果我检查isSpeaking属性,它仍然是NO。如何才能让音频从中断的位置重新开始播放?

代码语言:javascript
复制
AVSpeechSynthesizer *synthesizer_;

synthesizer_ = [[AVSpeechSynthesizer alloc] init];
synthesizer_.delegate = self;


- (void)textToSpeech{
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:itemText];
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:localeCode];
    utterance.rate = UTTERANCE_RATE;
    utterance.preUtteranceDelay = itemDelayTimeInterval;
    [synthesizer_ speakUtterance:utterance];
}

- (IBAction)pauseButtonPressed:(id)sender {
    if (synthesizer_.isSpeaking) {
        [synthesizer_ pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
    }
    else{
        [synthesizer_ continueSpeaking];
    }

}

EN

回答 4

Stack Overflow用户

发布于 2013-11-23 01:48:39

你的原始代码是:

代码语言:javascript
复制
if (synthesizer_.isSpeaking) {

试着这样做:

代码语言:javascript
复制
if (![synthesizer_ isPaused])

原因:

一个布尔值,指示合成器是否正在说话。(只读)

代码语言:javascript
复制
@property(nonatomic, readonly, getter=isSpeaking) BOOL speaking

如果合成器正在发言或有发言排队等待发言,则讨论返回YES,即使它当前处于暂停状态。如果合成器已完成其队列中的所有发声,或者尚未发出发声,则返回NO

在iOS 7.0及更高版本中可用。在AVSpeechSynthesis.h中声明

因此,如果使用此属性,并且语音已暂停,则此属性可能为true,并且语音继续代码将不会运行。

票数 1
EN

Stack Overflow用户

发布于 2015-07-12 00:05:30

只要从isPaused开始,它就会工作得很好:

注意:使用AVSpeechBoundaryImmediate代替AVSpeechBoundaryWord可以获得更好的效果。

代码语言:javascript
复制
- (IBAction)pauseButtonPressed:(id)sender {
    if (synthesizer_.isPaused) {
        [synthesizer_ continueSpeaking];
    }
    else{
        [synthesizer_ pauseSpeakingAtBoundary: AVSpeechBoundaryImmediate];
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-12-08 12:28:27

尝试使用以下代码

代码语言:javascript
复制
-(void)stopSpeechReading
{
    if([synthesize isSpeaking]) {
        NSLog(@"Reading has been stopped");
        [synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
        [synthesize speakUtterance:utterance];
        [synthesize stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}
-(void)pauseSpeechReading
{
    if([synthesize isSpeaking]) {
        NSLog(@"Reading paused");
        [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@""];
        [synthesize speakUtterance:utterance];
        [synthesize pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    }
}
-(void)resumeSpeechReading
{
     NSLog(@"Reading resumed");
    [synthesize continueSpeaking];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19367670

复制
相关文章

相似问题

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