Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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;
if ([inUserData isKindOfClass:[IVAudioPlayerController class]]) {
IVAudioPlayerController *controller = (IVAudioPlayerController *) inUserData;
// if application sound is not playing, there's nothing to do, so return.
if (controller.audioStreamer.state == AS_STOPPED ) {
TTDPRINT(@"Audio route change while application audio is stopped.");
} else {
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue ( routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason) );
SInt32 routeChangeReason;
CFNumberGetValue ( routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason );
// "Old device unavailable" indicates that a headset was unplugged, or that the
// device was removed from a dock connector that supports audio output. This is
// the recommended test for when to pause audio.
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
TTDPRINT(@"Output device removed, so application audio was paused.");
[controller pause];
} else {
TTDPRINT(@"A route change occurred that does not require pausing of application audio.");

inailuy commented Jun 25, 2013

thanks! found it useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment