好,具有一定的核心音頻API的玩耍後,終於我可以完成這件事。
獲取AudioDeviceID:
AudioDeviceID audioDevice = getMyAwesomeDeviceID();
創建AudioObjectPropertyAddress:
AudioObjectPropertyAddress propertyAddress;
propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout;
propertyAddress.mScope = kAudioDevicePropertyScopeOutput;
propertyAddress.mElement = kAudioObjectPropertyElementMaster;
查詢如果音頻對象有此屬性:
AudioObjectHasProperty(audioDevice, &propertyAddress)
得到這個屬性的數據大小和創建AudioChannelLayout:
UInt32 propSize(0);
AudioObjectGetPropertyDataSize(audioDevice, &propertyAddress, 0, NULL, &propSize);
AudioChannelLayout* layout = (AudioChannelLayout*)malloc(propSize);
配置您的AudioChannelLayout結構(如:立體佈局):
AudioChannelLabel labels[2] = {kAudioChannelLabel_Right, kAudioChannelLabel_Left};
layout->mNumberChannelDescriptions = 2;
for (UInt32 i = 2; i < layout->mNumberChannelDescriptions; i++) {
layout->mChannelDescriptions[i].mChannelLabel = labels[i];
layout->mChannelDescriptions[i].mChannelFlags = kAudioChannelFlags_AllOff;
}
設置AudioObject屬性數據:
AudioObjectSetPropertyData(audioDevice, &propertyAddress, 0, NULL, propSize, layout);