【发布时间】:2021-05-03 22:33:27
【问题描述】:
我正在尝试使用录音机来捕捉来自 AirPods Pro 的输入。以下函数执行音频捕获,它适用于内置麦克风,但不适用于 AirPods。该问题在模拟器和真实设备上都存在。有人可以指出我在这里缺少什么来启用蓝牙捕获吗?谢谢!
func startRecording() {
let recordingSession = AVAudioSession.sharedInstance()
// Then we start recording session with
do {
try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker, .allowBluetoothA2DP])
try recordingSession.setActive(true)
} catch {
print("Failed to set up recording session")
}
// Then we say where to save it
let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// file is named after the date and time of the recording in the .m4a format.
let audioFilename = documentPath.appendingPathComponent("\(Date().toString( dateFormat: "dd-MM-YY_'at'_HH:mm:ss")).m4a")
let settings = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 48000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.record()
recording = true
} catch {
print("Could not start recording")
}
} //end func startRecording
【问题讨论】:
-
有什么帮助吗?
-
您好!我知道这是不被接受的。但是很长一段时间我无法解决这个问题。你可以帮帮我吗?我会很感激。我在我所在的地区找不到 AirPods Pro。你能打开我的小xcode项目并测试一下吗? stackoverflow.com/questions/69851479/…
标签: swift xcode avfoundation