使用蓝牙耳机时,AVAudioEngine 位置音频不适用于 .playAndRecord 音频会话类别
AVAudioEngine positional audio not working with .playAndRecord audio session category when using bluetooth headphones
我希望能够同时录制音频和播放定位音频。
为此,我需要使用 .playAndRecord
音频会话类别,同时进行录音和回放。但是,使用此类别时,在使用蓝牙耳机时,音频文件会在没有定位的情况下播放(即它不是空间的)。这在使用有线耳机时按预期工作。
如果我将音频会话类别设置为 .playback
,播放的音频对于有线和蓝牙耳机的位置都是正确的,但是我无法同时录制。
我试过各种音频会话 categories/option 但没有成功。
import AVFoundation
class PlayerRecorder: ObservableObject {
let engine = AVAudioEngine()
let mixer = AVAudioEnvironmentNode()
init() {
let audioSession = AVAudioSession.sharedInstance()
/*
Using .playAndRecord both recording and playback works, however
the audio that is played is NOT positional. .allowBluetooth is needed
so that bluetooth headphones can be used.
*/
try! audioSession.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth)
/*
Using .playback the positional audio DOES work, however we are not able to record.
*/
// try! audioSession.setCategory(.playback)
self.engine.attach(self.mixer)
let stereoFormat = AVAudioFormat(standardFormatWithSampleRate: self.engine.outputNode.outputFormat(forBus: 0).sampleRate, channels: 2)
self.engine.connect(self.mixer, to: self.engine.outputNode, format: stereoFormat)
self.engine.prepare()
try! self.engine.start()
}
func play() {
let audioPlayer = AVAudioPlayerNode()
self.engine.attach(audioPlayer)
let monoFormat = AVAudioFormat(standardFormatWithSampleRate: self.engine.outputNode.outputFormat(forBus: 0).sampleRate, channels: 1)
self.engine.connect(audioPlayer, to: self.mixer, format: monoFormat)
// This file has to be in mono
let url = Bundle.main.url(forResource: "your-mono-audio-file.mp3", withExtension: nil)
let f = try! AVAudioFile(forReading: url!)
audioPlayer.scheduleFile(f, at: nil, completionHandler: nil)
audioPlayer.renderingAlgorithm = .HRTFHQ
audioPlayer.position = AVAudio3DPoint(x: 20.0, y: 5.0, z: 0.0)
audioPlayer.play()
}
}
I want to be able to record audio and play back positional audio at the same time.
如果您同时使用蓝牙耳机播放和录音,则无法通过标准蓝牙实现。选项 allowBluetooth
并不意味着“允许蓝牙”。它的意思是“如果可用,首选 HFP”。 (这是我在 Core Bluetooth 中知道的最糟糕的命名常量。)HFP 是一种低带宽双向音频协议,专为 phone 呼叫而设计。
如果切换到高带宽音频协议 A2DP,您会发现它是单向的,因此您无法从 microphone.
没有广泛部署的蓝牙协议既能提供高质量音频又能访问微型phone。如果您控制固件,则可以通过 BLE(如果是 MFi 设备,则为 iAP2)开发自己专有的 microphone 音频流。但除此之外,目前没有解决方案。
我一直希望 LEA will fix this, but I can't find any hint that it will. I also had hoped aptX 可以修复它(即使 iPhone 不支持它),但也没有运气。我不确定为什么蓝牙委员会和供应商没有处理这个用例,但据我所知,目前还没有任何进展。
我希望能够同时录制音频和播放定位音频。
为此,我需要使用 .playAndRecord
音频会话类别,同时进行录音和回放。但是,使用此类别时,在使用蓝牙耳机时,音频文件会在没有定位的情况下播放(即它不是空间的)。这在使用有线耳机时按预期工作。
如果我将音频会话类别设置为 .playback
,播放的音频对于有线和蓝牙耳机的位置都是正确的,但是我无法同时录制。
我试过各种音频会话 categories/option 但没有成功。
import AVFoundation
class PlayerRecorder: ObservableObject {
let engine = AVAudioEngine()
let mixer = AVAudioEnvironmentNode()
init() {
let audioSession = AVAudioSession.sharedInstance()
/*
Using .playAndRecord both recording and playback works, however
the audio that is played is NOT positional. .allowBluetooth is needed
so that bluetooth headphones can be used.
*/
try! audioSession.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth)
/*
Using .playback the positional audio DOES work, however we are not able to record.
*/
// try! audioSession.setCategory(.playback)
self.engine.attach(self.mixer)
let stereoFormat = AVAudioFormat(standardFormatWithSampleRate: self.engine.outputNode.outputFormat(forBus: 0).sampleRate, channels: 2)
self.engine.connect(self.mixer, to: self.engine.outputNode, format: stereoFormat)
self.engine.prepare()
try! self.engine.start()
}
func play() {
let audioPlayer = AVAudioPlayerNode()
self.engine.attach(audioPlayer)
let monoFormat = AVAudioFormat(standardFormatWithSampleRate: self.engine.outputNode.outputFormat(forBus: 0).sampleRate, channels: 1)
self.engine.connect(audioPlayer, to: self.mixer, format: monoFormat)
// This file has to be in mono
let url = Bundle.main.url(forResource: "your-mono-audio-file.mp3", withExtension: nil)
let f = try! AVAudioFile(forReading: url!)
audioPlayer.scheduleFile(f, at: nil, completionHandler: nil)
audioPlayer.renderingAlgorithm = .HRTFHQ
audioPlayer.position = AVAudio3DPoint(x: 20.0, y: 5.0, z: 0.0)
audioPlayer.play()
}
}
I want to be able to record audio and play back positional audio at the same time.
如果您同时使用蓝牙耳机播放和录音,则无法通过标准蓝牙实现。选项 allowBluetooth
并不意味着“允许蓝牙”。它的意思是“如果可用,首选 HFP”。 (这是我在 Core Bluetooth 中知道的最糟糕的命名常量。)HFP 是一种低带宽双向音频协议,专为 phone 呼叫而设计。
如果切换到高带宽音频协议 A2DP,您会发现它是单向的,因此您无法从 microphone.
没有广泛部署的蓝牙协议既能提供高质量音频又能访问微型phone。如果您控制固件,则可以通过 BLE(如果是 MFi 设备,则为 iAP2)开发自己专有的 microphone 音频流。但除此之外,目前没有解决方案。
我一直希望 LEA will fix this, but I can't find any hint that it will. I also had hoped aptX 可以修复它(即使 iPhone 不支持它),但也没有运气。我不确定为什么蓝牙委员会和供应商没有处理这个用例,但据我所知,目前还没有任何进展。