在 NSSound 被证明无法胜任这项任务之后,我在上周计划外进行了一次深入 Macintosh 音响系统的深入探索。我终于用音频队列服务播放了我的文件,现在只剩下一件小事要做了:切换输出设备。
不幸的是,似乎我做错了什么,或者您应该通过的设备 UID CFStringRef 不是 Core Audio 给出的那个..
下面的代码检索标准输出设备(默认情况下音频队列将播放到该设备,但它拒绝更改设备:
UInt32 thePropSize;
AudioDeviceID defaultAudioDevice;
OSStatus result = noErr;
// get the device list
AudioObjectPropertyAddress thePropertyAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, &thePropSize);
result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, &thePropSize, &defaultAudioDevice);
CFStringRef theDeviceName;
// get the device name
thePropSize = sizeof(CFStringRef);
thePropertyAddress.mSelector = kAudioObjectPropertyName;
thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
// get the name of the device
result = AudioObjectGetPropertyData( (AudioObjectID)defaultAudioDevice,
&thePropertyAddress, 0, NULL, &thePropSize, &theDeviceName);
// get the uid of the device
CFStringRef theDeviceUID;
thePropertyAddress.mSelector = kAudioDevicePropertyDeviceUID;
result = AudioObjectGetPropertyData( (AudioObjectID)defaultAudioDevice,
&thePropertyAddress, 0, NULL, &thePropSize, &theDeviceUID);
result = AudioQueueSetProperty( playerState.mQueue,
kAudioQueueProperty_CurrentDevice,
&theDeviceUID,
sizeof(theDeviceUID));
如果队列正在播放,我会收到错误 kAudioQueueErr_InvalidRunState,告诉我在队列正在播放时无法设置此属性。如果队列没有播放,我会得到 -50 参数错误。
我的指针有问题吗?还是某处有不同的设备uid!?
任何帮助将不胜感激。