2011-07-19 29 views

Respuesta

9

Esto puede ser un poco complicado porque algunos dispositivos de audio admiten un canal maestro, pero la mayoría no lo hace, por lo que el volumen será una propiedad por canal. Dependiendo de lo que necesite hacer, podría observar solo un canal y asumir que todos los demás canales que admite el dispositivo tienen el mismo volumen. Independientemente del número de canales que desea ver, se observa el volumen mediante el registro de un oyente propiedad para el AudioObject en cuestión:

// Some devices (but not many) support a master channel 
AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput, 
    kAudioObjectPropertyElementMaster 
}; 

if(AudioObjectHasProperty(deviceID, &propertyAddress)) { 
    OSStatus result = AudioObjectAddPropertyListener(deviceID, &propertyAddress, myAudioObjectPropertyListenerProc, self); 
    // Error handling omitted 
} 
else { 
    // Typically the L and R channels are 1 and 2 respectively, but could be different 
    propertyAddress.mElement = 1; 
    OSStatus result = AudioObjectAddPropertyListener(deviceID, &propertyAddress, myAudioObjectPropertyListenerProc, self); 
    // Error handling omitted 

    propertyAddress.mElement = 2; 
    result = AudioObjectAddPropertyListener(deviceID, &propertyAddress, myAudioObjectPropertyListenerProc, self); 
    // Error handling omitted 
} 

Su proc oyente debe ser algo como:

static OSStatus 
myAudioObjectPropertyListenerProc(AudioObjectID       inObjectID, 
            UInt32        inNumberAddresses, 
            const AudioObjectPropertyAddress  inAddresses[], 
            void         *inClientData) 
{ 
    for(UInt32 addressIndex = 0; addressIndex < inNumberAddresses; ++addressIndex) { 
     AudioObjectPropertyAddress currentAddress = inAddresses[addressIndex]; 

     switch(currentAddress.mSelector) { 
      case kAudioDevicePropertyVolumeScalar: 
      { 
       Float32 volume = 0; 
       UInt32 dataSize = sizeof(volume); 
       OSStatus result = AudioObjectGetPropertyData(inObjectID, &currentAddress, 0, NULL, &dataSize, &volume); 

       if(kAudioHardwareNoError != result) { 
        // Handle the error 
        continue; 
       } 

       // Process the volume change 

       break; 
      } 
     } 
    } 
} 
Cuestiones relacionadas