2011-07-19 15 views
5

ओएस एक्स ध्वनि वॉल्यूम बदल गया है जब मुझे अपने ऐप को अधिसूचित करने की आवश्यकता है। यह एक डेस्कटॉप ऐप के लिए है, आईओएस के लिए नहीं। मैं इस अधिसूचना के लिए कैसे पंजीकरण कर सकता हूं?ध्वनि वॉल्यूम परिवर्तन के लिए मैं अधिसूचना के लिए कैसे पंजीकरण करूं?

उत्तर

9

यह एक छोटा सा मुश्किल हो सकता है क्योंकि कुछ ऑडियो डिवाइस एक मास्टर चैनल का समर्थन करते हैं, लेकिन अधिकांश ऐसा नहीं है कि वॉल्यूम प्रति चैनल संपत्ति होगी। आपको जो करने की आवश्यकता है उसके आधार पर आप केवल एक चैनल देख सकते हैं और मान लें कि डिवाइस के सभी अन्य चैनलों का समान मात्रा समान है। कितने चैनलों आप देखना चाहते हैं के बावजूद, आप प्रश्न में AudioObject के लिए एक संपत्ति श्रोता पंजीकरण से मात्रा पर ध्यान दें:

// 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 
} 

आपका श्रोता proc होना चाहिए कुछ की तरह:

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; 
      } 
     } 
    } 
} 
संबंधित मुद्दे