Skip to content

Instantly share code, notes, and snippets.

@edwardinubuntu
Created June 25, 2012 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardinubuntu/2988342 to your computer and use it in GitHub Desktop.
Save edwardinubuntu/2988342 to your computer and use it in GitHub Desktop.
audioRouteChangeListenerCallback
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.");
return;
} 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
Copy link

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