AVAudioSession不适用于某些蓝牙设备

时间:2018-06-11 00:44:49

标签: ios swift bluetooth avaudiosession

我遇到蓝牙播放问题但仅限于某些蓝牙设备。其他应用程序在这些相同的蓝牙设备上运行良好。我是否因为我为AVAudioSession设置类别而忽略了某些内容?

    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker, .allowBluetooth, .allowAirPlay])
        try session.setActive(true)

        session.requestRecordPermission({(granted:Bool) in
            if granted {
                Variables.appHasMicAccess = true
            } else {
                Variables.appHasMicAccess = false
            }
        })
    } catch let error as NSError {
        print("AVAudioSession configuration error: \(error.localizedDescription)")
    }

更新

解决方案是添加其他选项.allowBluetoothA2DP。这是在iOS 10中引入的。

  

从iOS 10.0开始,使用playAndRecord类别的应用也可以允许路由输出到配对的蓝牙A2DP设备。要启用此行为,您需要在设置音频会话类别时传递此类别选项。

更多详情here

1 个答案:

答案 0 :(得分:2)

如果播放在使用A2DP的扬声器上,请考虑如下设置会话:

let audioSession = AVAudioSession.sharedInstance()
do {
      try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, 
           mode: AVAudioSessionModeVideoRecording,
           options: [.defaultToSpeaker, .allowAirPlay, .allowBluetoothA2DP])
      try audioSession.setActive(true)
} catch let error {
      print("audioSession properties weren't set!", error)
}

注意选项“.allowBluetoothA2DP”

希望有所帮助