2012-04-03 3 views
2

Mac OS X 응용 프로그램에서 볼륨 컨트롤을 구현하려고합니다. 내가 맥북 프로에 설치된 오디오 장치를 반복하고 마스터 볼륨 (또는 개별 채널 볼륨 - 나는 그것을 시도했다)을 쿼리하는 코드를 실행하면 어떤 결과도 얻을 수 없다. 내가 로그에서 얻을오디오 장치의 출력 볼륨을 코코아 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/엑스 코드 4.3.2을 실행하는거야

장치의 볼륨이 실제로 0으로 설정되어 있지 않습니다. 아무도 왜 내가 값을 얻을 수 없다는 걸 말해 줄 수 있니?

답변

2

mSelector를 변경하여 propertyAddress가 올바르게 형성되었는지 잘 모르겠습니다.

AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput, 
    1 
}; 

을 그리고 최종 AudioObjectGetPropertyData 호출에서 그 사용이 값을 갖는 새 propertyAddress를 만들어보십시오.

+0

안녕하세요, 감사합니다. mSelector를 변경하면 장치 이름을 가져 오는 데 문제가없는 것처럼 보였습니다. 귀하가 보낸 코드는 몇 가지 장치에 대한 볼륨 값을 제공하지만 kAudioHardwareServiceDeviceProperty_VirtualMasterVolume이 아닌 kAudioDevicePropertyVolumeScalar를 지정했습니다. 나는 이유가 무엇이든간에 후자는 내 경우에는 좋은 선택 자라고 생각하지 않는다. 해당 propertyAddress의 1은 장치의 채널 1에 대한 레벨을 얻음을 의미합니까? – pjv

+1

예, 채널입니다. [Tech Note 2223] (http://developer.apple.com/library/mac/#technotes/tn2223/_index.html)에 몇 가지 예가 있습니다. – Nick

관련 문제