我试图在Swift中做一些相当基本的事情,即确定当前用于录制我的声音的设备。
(即我使用以下代码通过命令行应用程序录制声音)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
let recorder = try AVAudioRecorder(url: fileUrl, settings: settings);
audioRecorder.start();
如何从AVAudioRecorder获取详细信息告诉我"这是系统用于记录的默认输入设备"?即默认设备为"内置麦克风"或" Airpods"等。
答案 0 :(得分:0)
我设法通过使用Core Audio库解决了我的问题。以下是从当前选定的默认设备获取设备名称的代码:
func showDeviceName() {
// load the current default device
var deviceId = AudioDeviceID(0);
var deviceSize = UInt32(MemoryLayout.size(ofValue: deviceId));
var address = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDefaultInputDevice, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster);
var err = AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &address, 0, nil, &deviceSize, &deviceId);
if ( err == 0) {
// change the query property and use previously fetched details
address.mSelector = kAudioDevicePropertyDeviceNameCFString;
var deviceName = "" as CFString;
deviceSize = UInt32(MemoryLayout.size(ofValue: deviceName));
err = AudioObjectGetPropertyData( deviceId, &address, 0, nil, &deviceSize, &deviceName);
if (err == 0) {
print("### current default mic:: \(deviceName) ");
} else {
// TODO:: unable to fetch device name
}
} else {
// TODO:: unable to fetch the default input device
}
}