2012-04-03 4 views
2

私はMac OS Xアプリケーションで音量コントロールを実装しようとしています。 MacBook Proにインストールされたオーディオデバイスをループし、マスターボリューム(または個々のチャネルボリューム - これも試しました)をクエリするコードを実行すると、結果が得られません。ここオーディオデバイスからの出力音量をココアのobjective-cから得ることができません

は、私が実行しているコードです:

- (NSArray*)getAudioDevices 
{ 
    AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwarePropertyDevices, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
    }; 

    UInt32 dataSize = 0; 
    OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize); 
    if(kAudioHardwareNoError != status) 
    { 
    NSLog(@"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) 
    { 
    NSLog(@"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++) 
    {  

    // Query device name 
    CFStringRef deviceName = NULL; 
    dataSize = sizeof(deviceName); 
    propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString; 
    status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceName); 
    if(kAudioHardwareNoError != status) { 
     fprintf(stderr, "AudioObjectGetPropertyData (kAudioDevicePropertyDeviceNameCFString) failed: %i\n", status); 
     continue; 
    } 

    // Query device output volume 
    Float32 volume; 
    propertyAddress.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume; 
    status = AudioHardwareServiceHasProperty(audioDevices[i], &propertyAddress); 
    if(status) { 
     fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwareServiceDeviceProperty_VirtualMasterVolume) failed: %i\n", status); 
    } else { 
     dataSize = sizeof(volume); 
     status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &volume); 
     if (status) { 
     // handle error 
     } 
    } 

    NSLog(@"device found: %d - %@ || Vol: %f || status %i",audioDevices[i], deviceName, volume, status); 

    [devices addObject:[NSNumber numberWithInt:audioDevices[i]]]; 
    } 

    free(audioDevices); 

    return [NSArray arrayWithArray:devices]; 
} 

と私はログから取得した出力:

device found: 58 - Built-in Microphone || Vol: 0.000000 || status 2003332927 
device found: 78 - Built-in Input || Vol: 0.000000 || status 2003332927 
device found: 68 - Built-in Output || Vol: 0.000000 || status 2003332927 
device found: 54 - Microsoft LifeCam VX-5000 || Vol: 0.000000 || status 2003332927 
device found: 87 - Microsoft LifeChat LX-3000 || Vol: 0.000000 || status 2003332927 

私はOS X 10.7.3/Xcodeの4.3.2を実行していますよ

デバイス上のボリュームは実際にはゼロに設定されていません。誰も私が何か値を得ることができない理由を教えてもらえますか?

答えて

2

mSelectorを変更するだけでpropertyAddressが正しく形成されているかわかりません。これらの値を使用して新しいpropertyAddressを作成してみてください:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput, 
    1 
}; 

最後のAudioObjectGetPropertyData呼び出しでこれを使用してください。

+0

ねえ、ありがとう。 mSelectorを変更すると、デバイス名を取得できたようです。あなたが送信したコードは、kAudioHardwareServiceDeviceProperty_VirtualMasterVolumeではなくkAudioDevicePropertyVolumeScalarを指定したにもかかわらず、いくつかのデバイスのボリューム値を返します。私は、私の場合、何らかの理由で後者が良いセレクターではないと思う。そのpropertyAddressの1は、デバイスのチャンネル1のレベルを取得していることを意味しますか? – pjv

+1

はい、それがチャンネルです。 [Tech Note 2223](http://developer.apple.com/library/mac/#technotes/tn2223/_index.html)にいくつかの例があります。 – Nick

関連する問題