我希望能够录制音频和播放位置音频在同一时间。
要做到这一点,我需要使用.playAndRecord
音频会话类别,同时录制和播放作品。然而,在使用蓝牙耳机时,使用此类别播放的音频文件并不是定位的(即不是空间的)。这与使用有线耳机时的预期效果一样。
如果我将音频会话类别设置为.playback
,则音频播放对于有线耳机和蓝牙耳机都是正确的位置,但是我无法同时录制。
我尝试过各种音频会话类别/选项,但没有运气。
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()
}
}
发布于 2021-02-11 23:20:31
我希望能够录制音频和播放位置音频在同一时间。
这是不可能的标准蓝牙,如果你同时播放和录音的蓝牙耳机。选项allowBluetooth
并不意味着“允许蓝牙”。意思是“如果可用的话更喜欢HFP”。(这是我在核心蓝牙中知道的最糟糕的常量。)HFP是一种低带宽双向音频协议,用于电话通话.
如果你切换到高带宽音频协议A2DP,你会发现它是单向的,所以你不能用麦克风录制。
没有广泛部署的蓝牙协议,使您既可以获得高质量的音频和麦克风。如果您控制固件,您可以开发自己的专有麦克风音频流的BLE (或iAP2,如果它是MFi设备)。但否则,就没有目前的解决办法。
我一直希望利娅能解决这个问题,但我找不到任何迹象表明它会解决这个问题。我还希望aptX可以修复它(尽管iPhones不支持它),但也没有什么好运气。我不知道为什么蓝牙委员会和供应商没有在处理这个用例,但据我所知,什么都没有出现。
https://stackoverflow.com/questions/66145794
复制相似问题