ios - AVAudioSession 在某些蓝牙设备上不工作

标签 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 中引入的。

Starting with iOS 10.0, apps using the playAndRecord category may also allow routing output to paired Bluetooth A2DP devices. To enable this behavior, you need to pass this category option when setting your audio session's category.

更多详情 here

最佳答案

如果在使用 A2DP 的扬声器上播放,请考虑这样设置 session :

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”

希望对你有帮助

关于ios - AVAudioSession 在某些蓝牙设备上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50789276/

相关文章:

iOS:HTTP Live Streaming 到应用程序,视频无法播放

ios - UIView 动画在 UITableViewCell 内无法正常工作

javascript - 使用 Eric Smekens node-bluetooth-obd 的 ELM327 蓝牙 OBD-II 适配器

objective-c - Mac OSX 上的串行端口配置文件 (SPP) 通信

ios - 通知后端推送通知已被禁用的最佳方式是什么?

ios - 在 Swift 4 中使用滑动操作删除文件

swift - 为什么不能为计算属性定义属性观察器?

ios - 检查应用程序更新时读取 json 错误

java - 如何使用蓝牙模块 HC-05 从 Arduino-uno 发送数据并在 Android 中读取?

ios - UITableView 自己滚动