首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioSession和AirPods

AVAudioSession和AirPods
EN

Stack Overflow用户
提问于 2017-12-14 15:21:12
回答 2查看 3.7K关注 0票数 5

我的应用程序似乎不适用于AirPods。现在,我正在使用这段代码来播放和录制:

代码语言:javascript
复制
    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 (我想用最少的迭代来完成这个任务)。

更新:(非常天真的方法--但如果有蓝牙耳机,我只需要使用它们):

代码语言:javascript
复制
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")
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-06-23 04:44:08

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

[AVAudioSessionCategoryOptions.defaultToSpeaker, .allowBluetoothA2DP]

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

票数 5
EN

Stack Overflow用户

发布于 2020-10-27 15:56:24

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

我申请了Swift 5

代码语言:javascript
复制
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)
    }
  }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47816753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档