检测耳机被拔出

导入

#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

1、viewDidLoad方法里加入

AudioSessionAddPropertyListener (

                                     kAudioSessionProperty_AudioRouteChange,

                                     audioRouteChangeListenerCallback,

                                     self

                                     );

2、实现回调

 

#pragma mark -

#pragma mark Audio route change listener callback

 

// Audio session callback function for responding to audio route changes. If playing back audio and

//   the user unplugs a headset or headphones, or removes the device from a dock connector for hardware

//   that supports audio playback, this callback detects that and stops playback.

//

// Refer to AudioSessionPropertyListener in Audio Session Services Reference.

void audioRouteChangeListenerCallback (

                                       void                      *inUserData,

                                       AudioSessionPropertyID    inPropertyID,

                                       UInt32                    inPropertyValueSize,

                                       const void                *inPropertyValue

                                       )

{

    

    // Ensure that this callback was invoked because of an audio route change

    if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;

    

    // This callback, being outside the implementation block, needs a reference to the MixerHostAudio

    //   object, which it receives in the inUserData parameter. You provide this reference when

    //   registering this callback (see the call to AudioSessionAddPropertyListener).

    ViewController  *controller = (ViewController *) inUserData;    

// if application sound is not playing, there's nothing to do, so return.

    if (MPMoviePlaybackStateStopped == controller.MusicaudioPlayer.playbackState||MPMoviePlaybackStatePaused == controller.MusicaudioPlayer.playbackState) {        

        NSLog (@"Audio route change while application audio is stopped.");

        return;        

    } else {  

        // Determine the specific type of audio route change that occurred.

        CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;       

        CFNumberRef routeChangeReasonRef = 

        (CFNumberRef)CFDictionaryGetValue (

                              routeChangeDictionary,

                              CFSTR (kAudioSession_AudioRouteChangeKey_Reason)

                              );        

        SInt32 routeChangeReason;

        CFNumberGetValue (

                          routeChangeReasonRef,

                          kCFNumberSInt32Type,

                          &routeChangeReason

                          );

        // "Old device unavailable" indicates that a headset or headphones were unplugged, or that

        //    the device was removed from a dock connector that supports audio output. In such a case,

        //    pause or stop audio (as advised by the iOS Human Interface Guidelines).

        if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {

            [controller playOrPauseMusic];

        } else {

            NSLog (@"A route change occurred that does not require stopping application audio.");

        }

    }

}

posted @ 2012-09-04 15:02  ValeTu  阅读(463)  评论(0编辑  收藏  举报