To get the headphone plug-in plug-out event we need to employ a property listener callback function. The system invokes the callback when a user plugs in or unplugs a headset, or docks or undoes the device – thereby adding or removing an audio connection. The system also invokes the property listener callback when a Bluetooth device connects or disconnects.
Defining a Property Listener Callback Function
To respond to a route change, a property listener callback function must:
- Identify the nature of the route change
- Branch, depending on the specific route change and the current audio context (for example, recording, playback, or stopped)
- Take or invoke appropriate action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | void audioRouteChangeListenerCallback ( void *inUserData, // 1 AudioSessionPropertyID inPropertyID, // 2 UInt32 inPropertyValueSize, // 3 const void *inPropertyValue // 4 ) { if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return ; // 5 MainViewController *controller = (MainViewController *) inUserData; // 6 if (controller.appSoundPlayer.playing == 0 ) { // 7 return ; } else { CFDictionaryRef routeChangeDictionary = inPropertyValue; // 8 CFNumberRef routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary,CFSTR (kAudioSession_AudioRouteChangeKey_Reason)); SInt32 routeChangeReason; CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason); if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) { // 9 [controller.appSoundPlayer pause]; UIAlertView *routeChangeAlertView = [[UIAlertView alloc] initWithTitle: @"Playback Route Changed" message: @"Audio output was changed." delegate: nil cancelButtonTitle: @"Ok" otherButtonTitles: nil ]; [routeChangeAlertView show]; } } } |
- A pointer to data that you provide when initializing your audio session.In an Objective-C class file, such as a view controller class, you place the property listener callback function outside of the class implementation block. Because of this, the callback needs a reference to the controller object to be able to send messages to it. You provide this reference when initializing your audio session.
- The identifier for the property that this callback function gets notified about.
- The size, in bytes, of the data in the inPropertyValue parameter.
- The current value of the property that this callback function is monitoring. Because the property being monitored is the kAudioSessionProperty_AudioRouteChange property, this value is a CFDictionary object.
- Ensures that the callback was invoked for the correct audio session property change.
- Initializes a MainViewController object instance to the reference passed in by the inUserData parameter. This allows the callback to send messages to your view controller object—typically defined in the same file that implements the callback.
- If application sound is not playing, there's nothing to do, so return.
- This line and the next several lines determine the reason for the route change. The one route change of interest in this playback-only example is that an output device, such as a headset, was removed.
- If an output device was indeed removed, then pause playback and display an alert that allows the user to stop or resume playback.
Your application can listen for hardware and route change events by way of the property mechanism in Audio Session Services. For example, to listen for route change events, you register a callback function with your audio session object, as shown here.
1 2 3 4 5 6 7 8 9 10 11 | AudioSessionPropertyID routeChangeID = kAudioSessionProperty_AudioRouteChange; // 1 AudioSessionAddPropertyListener ( // 2 routeChangeID, // 3 audioRouteChangeListenerCallback, // 4 userData // 5 ); |
- Declares and initializes a variable to the identifier for the property you want to monitor.
- Registers your property listener callback function with your initialized audio session.
- The identifier for the property you want to monitor.
- A reference to your hardware listener callback function.
- Data you want the audio session to pass back to your callback function.
Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.
No comments:
Post a Comment