【问题标题】:AVAudioSession and AirPodsAVAudioSession 和 AirPods
【发布时间】:2017-12-14 15:21:12
【问题描述】:

我的应用似乎不适用于 AirPods。现在我正在使用此代码进行播放和录制:

    let audioSession = AVAudioSession.sharedInstance()
    do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
    }catch {
        print("audioSession properties weren't set because of an error.")
    }

我把defaultToSpeaker改成allowBluetooth就够了吗?

附:我知道这是一个非常愚蠢的问题,因为更改这条线并检查会更简单,但我现在没有 AirPods,所以我唯一的选择是将新版本上传到 Testflight(和我想以最少的迭代次数来做到这一点)。

更新:(相当幼稚的方法——但我只需要使用可用的蓝牙耳机):

func selectDevice(audioSession: AVAudioSession) {

    var headphonesExist = false
    var bluetoothExist = false
    var speakerExist = false

    let currentRoute = AVAudioSession.sharedInstance().currentRoute

    for output in audioSession.currentRoute.outputs {
        print(output)

        if output.portType == AVAudioSessionPortHeadphones || output.portType == AVAudioSessionPortHeadsetMic {
            headphonesExist = true
        }

        if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP {
            bluetoothExist = true
        }

        if output.portType == AVAudioSessionPortBuiltInSpeaker {
            speakerExist = true
        }
    }

    if bluetoothExist == true {
        do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) } catch {
            print("error with audiosession: bluetooth")
        }
    }
}

【问题讨论】:

    标签: ios swift audio avfoundation avaudiosession


    【解决方案1】:

    您需要在options 参数中添加对蓝牙的支持,如下所示:

    [AVAudioSessionCategoryOptions.defaultToSpeaker, .allowBluetoothA2DP]

    .allowBluetoothA2DP 将允许高质量音频输出到蓝牙设备并限制所述设备上的麦克风输入,而.allowBluetooth 将默认 HFP 兼容(输入/输出)蓝牙设备为支持麦克风输入的低质量 HFP .

    【讨论】:

    • 在我的情况下不起作用。我想同时录音和播放。但是当我尝试启动音轨时,AirPods 会作为允许的音频 io 设备消失。 allowBluetoothA2DP 对我没有帮助。
    • 经过几天的测试,我可以确认这一点。答案是正确的。 A2DP 是目前最好的解决方案。根据我的经验,HFP 太糟糕了。
    【解决方案2】:

    这是请求适当许可的完整来源。 您所要做的就是使用“.allowBluetoothA2DP”添加一个模式

    我已经申请了 Swift 5

    func requestPermissionForAudioRecording(){
        recordingSession = AVAudioSession.sharedInstance()
        
        do {
          // only with this without options, will not capable with your Airpod, the Bluetooth device.
          //  try recordingSession.setCategory(.playAndRecord, mode: .default)
          try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowAirPlay, .allowBluetoothA2DP])
          try recordingSession.setActive(true)
          recordingSession.requestRecordPermission() { allowed in
            DispatchQueue.main.async {
              if allowed {
                // Recording permission has been allowed
                self.recordingPermissionGranted = true
              } else {
                // failed to record!
              }
            }
          }
        } catch let err {
          // failed to record!
          print("AudioSession couldn't be set!", err)
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多