【问题标题】:AVSpeechSynthesizer in background mode后台模式下的 AVSpeechSynthesizer
【发布时间】:2013-10-04 14:19:20
【问题描述】:

当我的 iOS 应用程序处于后台模式时,我无法让 iOS 7 AVSpeechSynthesizer 工作。我已将“应用播放音频”键添加到应用支持的后台模式中,但我仍然无法让它工作。

我还研究了创建带有AVSpeechSynthesizer 话语的AVMutableCompositionTrack 的可能性,然后以某种方式与能够在后台运行的玩家一起播放它 - 但没有运气。

在后台使用AVSpeechSynthesizer 有没有人比我运气好?

【问题讨论】:

    标签: ios objective-c ios7 avfoundation


    【解决方案1】:
    1. 您必须在后台模式下设置“音频和 AirPlay”。
    2. 您必须配置音频会话:
        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
        }
    

    【讨论】:

    • 这真的可以让你在音频背景模式下播放吗?
    • @ChrisTruman,我可以确认它有效。 imihaly,“会话 setActive”调用是否会导致您的应用程序继续在后台运行并增加应用程序的电源需求?我会做一些测试,但我的猜测是答案是肯定的,所以您需要拨打上述电话,播放合成语音,然后在完成后将会话的活动状态设置回 NO。
    • @imihaly,我已经使用了你的代码并且它有效。 (+1) 根据我的测试,该应用似乎没有在后台运行。但是,我正在开发的应用程序旨在长时间在后台运行,并且可能会在其生命周期内反复暂停。有时语音合成会因为我不明白的原因停止工作。有什么想法吗?
    • 我已经这样做了,但是当屏幕从空闲状态变暗时,我的应用程序仍然停止说话。嗯。
    • 在 8.3 模拟器中为我工作。
    【解决方案2】:

    对于 swift 3,导入 AVKit(或 AVFoundation)然后添加

    try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    

    查看WillAppear()。无论静音开关状态如何,都可以在屏幕关闭的情况下在后台播放音频。

    *编辑:AVAudioSession 是在 AVFoundation 中定义的,也可以在 AVKit 中使用

    *编辑 2:自动完成的屏幕截图显示 AVAudioSession 在 AVKit 中可用

    【讨论】:

    • AVKit 必须替换为 AVFoundation 并在后台模式下设置“Audio and AirPlay”显然仍然需要
    • AVKit 是更高级别的框架,AVFoundation 是用于更细粒度控制的框架,但AVKit 绝不会被AVFoundation 取代。
    • 我并不是说 AVFoundation 是 AVKit 的替代品。我只是说 AVAudioSession 不包含在 AVKit 中,而是包含在 AVFoundation 中。因此,在这种情况下,您应该包括 AVFoundation。
    【解决方案3】:

    这段代码在 Swift 5 中适用于我

        do {
           try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: AVAudioSession.CategoryOptions.mixWithOthers)
           try AVAudioSession.sharedInstance().setActive(true)
        } catch {
            print(error)
        }
    
        let utterance = AVSpeechUtterance(string: voiceOutdata)
    
    
        let synth = AVSpeechSynthesizer()
        synth.speak(utterance)
    

    根据https://forums.developer.apple.com/thread/38917

    当会话可混合时,AVAudioSessionCategoryOptionMixWithOthers 与播放类别一起添加,在允许播放音频之前,会针对应用程序“为什么”处于唤醒状态进行一些内部检查。

    【讨论】:

      【解决方案4】:

      @imihaly 告诉“您必须在后台模式中设置“音频和 AirPlay”。” 可以理解

      您需要从那里启用它, 其次,在 AVSpeechSynthesizer 代码部分,您必须编写以下代码

       let audioSession = AVAudioSession.sharedInstance()
                  try! audioSession.setCategory(
                      AVAudioSession.Category.playback,
                      options: AVAudioSession.CategoryOptions.duckOthers
                  )
                       
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多