使用AVAudioSession时背景音频静音

时间:2019-04-13 17:23:44

标签: ios swift avaudiosession

我正在尝试通过音乐等背景音频来再现Snapchat的效果。进入他们的应用程序后,即使在录制时,它也保持正常音量。在我的应用程序中,打开应用程序后,它几乎立即静音。

我尝试在AVAudioSession设置类别函数中使用.mixWithOthers和.defaultToSpeaker选项。这些没有区别。

            if #available(iOS 10.0, *) {
                try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP, .defaultToSpeaker])
            } else {
                let options: [AVAudioSession.CategoryOptions] = [.mixWithOthers, .allowBluetooth, .defaultToSpeaker]
                              let category = AVAudioSession.Category.playAndRecord
                let selector = NSSelectorFromString("setCategory:withOptions:error:")
                AVAudioSession.sharedInstance().perform(selector, with: category, with: options)
            }
            try AVAudioSession.sharedInstance().setActive(true)
            session.automaticallyConfiguresApplicationAudioSession = false
        }
        catch {
            print("Failed to set background audio preference")

        }

1 个答案:

答案 0 :(得分:0)

  

在我的应用程序中,打开应用程序后,它几乎立即静音。

也许是因为您正在打开应用程序后立即配置和激活音频会话?如果那是问题,请不要这样做。仅在真正需要之前配置和激活音频会话。例如,在用户实际要求开始录音之前,请勿调整音频会话。在应用的整个生命周期内,根据需要更改音频会话类别都是