2012-01-20 78 views
7

所以這裏是怎麼回事。核心音頻和幻像設備ID

我正在嘗試使用Core Audio,特別是輸入設備。我想靜音,改變音量等等,我遇到了一些我無法想象的奇怪的東西。迄今爲止,谷歌一直沒有幫助。

當我查詢系統並詢問所有音頻設備的列表時,我返回了一個設備ID數組。在這種情況下,261,259,263,257

使用kAudioDevicePropertyDeviceName,我得到如下:

261:內置麥克風
259:內置輸入
263:內置輸出
257:iPhoneSimulatorAudioDevice

這一切都很好。

// This method returns an NSArray of all the audio devices on the system, both input and 
// On my system, it returns 261, 259, 263, 257 
- (NSArray*)getAudioDevices 
{ 
    AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwarePropertyDevices, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
    }; 

    UInt32 dataSize = 0; 
    OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize); 
    if(kAudioHardwareNoError != status) 
    { 
    MZLog(@"Unable to get number of audio devices. Error: %d",status); 
    return NULL; 
    } 

    UInt32 deviceCount = dataSize/sizeof(AudioDeviceID); 

    AudioDeviceID *audioDevices = malloc(dataSize); 

    status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices); 
    if(kAudioHardwareNoError != status) 
    { 
    MZLog(@"AudioObjectGetPropertyData failed when getting device IDs. Error: %d",status); 
    free(audioDevices), audioDevices = NULL; 
    return NULL; 
    } 

    NSMutableArray* devices = [NSMutableArray array]; 

    for(UInt32 i = 0; i < deviceCount; i++) 
    {  
    MZLog(@"device found: %d",audioDevices[i]); 
    [devices addObject:[NSNumber numberWithInt:audioDevices[i]]]; 
    } 

    free(audioDevices); 

    return [NSArray arrayWithArray:devices]; 
} 

當我查詢系統並詢問系統默認輸入設備的ID時,問題就出現了。此方法返回ID爲269,即在所有設備的數組中列出的而不是

如果我嘗試使用kAudioDevicePropertyDeviceName來獲取設備的名稱,則返回一個空字符串。雖然它沒有名稱,但如果我將此設備ID設爲靜音,則我的內置麥克風將靜音。相反,如果我將名爲「內置麥克風」的261 ID靜音,我的麥克風確實不會不是靜音。

// Gets the current default audio input device 
// On my system, it returns 269, which is NOT LISTED in the array of ALL audio devices 
- (AudioDeviceID)defaultInputDevice 
{ 
    AudioDeviceID defaultAudioDevice; 
    UInt32 propertySize = 0; 
    OSStatus status = noErr; 
    AudioObjectPropertyAddress propertyAOPA; 

    propertyAOPA.mElement = kAudioObjectPropertyElementMaster; 
    propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal; 
    propertyAOPA.mSelector = kAudioHardwarePropertyDefaultInputDevice; 
    propertySize = sizeof(AudioDeviceID); 

    status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &defaultAudioDevice); 

    if(status) 
    { //Error 
    NSLog(@"Error %d retreiving default input device",status); 
    return 0; 
    } 

    return defaultAudioDevice; 
} 

爲了進一步迷惑的東西,如果我手動切換我輸入到「行」與查詢默認的輸入設備,其上市的時候重新運行該程序,我得到的259的ID所有設備的陣列。

因此,要總結:

我試圖在我的系統中的輸入設備進行交互。如果我嘗試與設備ID 261(這是我的「內置麥克風」)交互,則不會發生任何事情。如果我嘗試與設備ID 269進行交互,顯然這是一個幻影ID,則我的內置麥克風會受到影響。當我查詢系統的默認輸入設備時,返回269 ID,但是當我向系統查詢所有設備的列表時,它未被列出。

有誰知道發生了什麼?我只是瘋了嗎?

在此先感謝!

回答

3

修正了它。

首先,幻像設備ID只是系統正在使用的虛擬設備。

其次,我無法靜音或對實際設備做任何事情的原因是因爲我使用AudioHardwareServiceSetPropertyData而不是AudioObjectSetPropertyData。

現在一切正常。