【问题标题】:AVSpeechSynthesizer not pausingAVSpeechSynthesizer 没有暂停
【发布时间】:2014-12-08 12:34:16
【问题描述】:

我在我的应用程序中使用 AVSpeechSynthesizer 进行 TTS。这些是我设置 iOS 7 TTS 的步骤。

#import <AVFoundation/AVFoundation.h>

@interface SpeakViewController : UIViewController <AVSpeechSynthesizerDelegate> {
    BOOL speechPaused;
}
@property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
@end

然后在我的 .m 中:

-(void)viewDidLoad
{
    speechPaused = NO;
    [self playSound];
}
// The sound is playing fine. 
-(void) playSound {
    speechPaused = NO;
    AVSpeechUtterance* utter = [[AVSpeechUtterance alloc] initWithString:_text];
    utter.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    [utter setRate:0.2f];
    if (!self.synthesizer) {
        self.synthesizer = [AVSpeechSynthesizer new];
    }
    self.synthesizer.delegate = self;
    [self.synthesizer speakUtterance:utter];
}
// This is what isn't working
- (void) pauseSpeech {
    if (speechPaused == NO) {
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        NSLog(@"Pause");
        speechPaused = YES;
    }
    if (self.synthesizer.speaking == NO) {
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@""];
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"];
        [self.synthesizer speakUtterance:utterance];
    }
}
- (void) playSpeech {
    [self.synthesizer continueSpeaking];
    speechPaused = NO;
    NSLog(@"Play");
}
- (IBAction)pauseSpeech:(id)sender {
    [self pauseSpeech];
}
- (IBAction)startSpeech:(id)sender {
    [self playSpeech];
}

我也在此方法中设置了一个 NSLog,但它从未被调用:

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
     NSLog(@"speechSynthesizer didPauseSpeechUtterance");
}

我已经被困在这个问题上好几天了。任何帮助表示赞赏。

【问题讨论】:

  • 曾经调用过pauseSpeech 方法吗?

标签: ios objective-c iphone ios7 text-to-speech


【解决方案1】:

试试这个

-(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];
}

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 2019-06-28
    • 2014-11-20
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2016-11-06
    相关资源
    最近更新 更多