2011-05-05 258 views

回答

7

如果您仍想潛水和亂用這個魔淵,我能夠建立一些共同組成我發現這裏的代碼:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/54013-hardware-volume-change-listener-callback.html

要註冊一個聽AudioProperties和捕獲有關'kAudioSessionProperty_AudioRouteChange'的任何消息。使用'理由'和'名字'可以解析收集發生的事情。您也可以閱讀更多有關在這裏:

http://developer.apple.com/library/ios/#DOCUMENTATION/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

// Registers this class as the delegate of the audio session. 
[[AVAudioSession sharedInstance] setDelegate: self]; 

// Use this code instead to allow the app sound to continue to play when the screen is locked. 
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; 

// Registers the audio route change listener callback function 
AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, self); 

回調:

void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) { 
    // ensure that this callback was invoked for a route change 
    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return; 


    { 
     // Determines the reason for the route change, to ensure that it is not 
     //  because of a category change. 
     CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue; 

     CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); 
     SInt32 routeChangeReason; 
     CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); 

     if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) { 

      //Handle Headset Unplugged 
     } else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) { 
        //Handle Headset plugged in 
     } 

    } 
} 
+0

謝謝,這正是我所需要的。 – David 2011-05-11 18:47:19

+4

這僅限iOS。 – junglecat 2013-03-31 10:03:32

+0

AudioSession API已在iOS 7.0中完全棄用 – 2014-03-27 01:24:13

5

這是「那些事」一:事情你應該永遠,永遠需要做什麼或知道。一般的想法是,你使用提供的API來播放聲音,而聲音子系統負責其餘部分。

如果您需要特定配置,您可以通過對話框要求用戶以特定方式親切配置他的系統,但就是這樣。其原因是一般的驅動程序編程和特別是聲音編程構成了很深的魔法,任何試圖以任何理由試圖纏繞機器硬件的應用程序通常都會失敗,但通常很微妙。

除非您正在爲已知的封閉機器開發企業應用程序,否則不要對機器硬件做出假設:在您知道它之前,下一個型號的iMac不帶模擬插孔。

即使模擬插孔存在且爲空,也可以通過次要聲卡(板載,PCI或USB)發出聲音。哎呀,即使有內存服務,甚至還有FireWire聲卡在外面飄蕩。

-3

這是一個隱藏的功能,存在(或不)在您的嵌入式芯片上。 如果製造商發佈一個API,你可以控制它,否則你不能。