7

AVSpeechSynthesizer在单例中使用。在iOS 8中,当应用程序在后台运行一段时间后,当它恢复时,AVSpeechSynthesizer单例将不再说话。这个问题在iOS 7上不会发生。

当应用程序进入后台时,我的日志中会显示以下消息:

AVSpeechSynthesizer Audio interruption notification: {
    AVAudioSessionInterruptionTypeKey = 1;
}

我在单例的方法中初始化AVSpeechSynthesizer这样的:init

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

utterance这样说:

AVSpeechUtterance *utt = [[AVSpeechUtterance alloc] initWithString:dialogue];
utt.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voice];

utt.pitchMultiplier = pitch;
utt.rate = rate;
utt.preUtteranceDelay = preDelay;
utt.postUtteranceDelay = postDelay;
utt.volume = volumeSetting;

[self.speechSynthesizer speakUtterance:utt];

有没有人在iOS 8上看到过类似的东西?

4

3 回答 3

13

我花了一整天的时间来追逐这种精神错乱,我想我已经找到了解决办法。我的问题是,它AVSpeechSynthesizer可以在前景和背景中正常工作,直到有电话发生时才回避其他音频。

在那一刻,说话将停止工作,没有任何错误。所有对象仍然存在,但委托调用不会被调用,既不会开始也不会结束。

我注意到,通过电话,我的应用程序会收到有关AudioRouteChanged. 因此,当这种情况发生时,我会重新创建语音设置:基本上破坏现有的AVSpeechSynthesizer并重新创建它。从那时起,说话将继续工作。它甚至可以在通话期间工作:)

于 2015-02-26T14:53:25.273 回答
2
  1. 您必须在后台模式下设置“音频和 AirPlay”。
  2. 您必须在 AppDelegate 中配置音频会话:

    NSError *error = NULL;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:&error];
    if(error) {
        // Do some error handling
    }
    [session setActive:YES error:&error];
    if (error) {
        // Do some error handling
    }
    

(见这篇文章:https ://stackoverflow.com/a/19200177/330067 )

于 2015-08-06T07:03:26.433 回答
0

经过进一步调查,当应用程序在后台启动时(由于后台提取或其他原因),似乎 AVSpeechSynthesizer 正在中断。一个简单的调用来在说话之前检查应用程序当前是否处于活动状态可以解决问题。

if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    [self.speechSynthesizer speakUtterance:utt];
于 2014-11-07T08:52:02.510 回答