在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参数错误。
我是在做错了指针吗?或者某个地方有不同的设备!?
非常感谢任何帮助。
答案 0 :(得分:2)
我找到了一个解决方案,我在这里发布档案:
Apple开发人员服务部门在一个单独的项目中对我的代码进行了测试,它立即为他们提供了很好的功能。不同之处在于他们设置了设备uid而没有所有繁琐的音频缓冲区和音量设置等。我移动了设备uid从队列设置结束更改为创建队列后立即和宾果游戏!它运作得很好。
我不是100%肯定,但我认为由于某些硬件驱动程序限制而设置队列的增益后,您无法更改设备。 “参数错误”似乎并没有指向那个方向,但我认为“更改设备太迟”错误会更合适。