Switch between earphones and speaker on iPhone

I am trying to configure audio routing to output an iPhone application. I use the route change listener to determine when the audio route changed. The listener detects changes, for example, when the headphones are plugged in and out. By default, the speaker produces sound, and then I plug in the headphones, and the sound is transmitted through the headphones in order. From there, no changes occur, even if the route change listener detects them.

Any help would be really appreciated.

NSError *sessionError = nil; [[AVAudioSession sharedInstance] setDelegate:self]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError]; UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(sessionCategory), &sessionCategory); AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, RouteChangeListener, (__bridge void *)(self)); AudioSessionSetActive(YES); 

Callback Receiver:

 void RouteChangeListener( void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) { if (inPropertyID == kAudioSessionProperty_AudioRouteChange) { CFStringRef newRoute; UInt32 size = sizeof(CFStringRef); AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute); if (newRoute) { CFShow(newRoute); if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), (UInt32)NULL) == kCFCompareEqualTo) { UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride); } else if (CFStringCompare(newRoute, CFSTR("HeadphonesAndMicrophone"), (UInt32)NULL) == kCFCompareEqualTo) { UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride); [[[AVAudioSession sharedInstance] player] play]; } } } } 
+1
ios avfoundation audio avplayer audiotoolbox
source share

No one has answered this question yet.

See similar questions:

nine
Play audio through the top (phone call) speaker

or similar:

1818
What is the difference between atomic and non-atomic attributes?
1300
Transferring data between view controllers
1141
How can I develop for iPhone using a Windows development machine?
nine
Play audio through the top (phone call) speaker
7
Detecting Connected iOS Audio Devices
4
iOS: Audio IN via connector, audio output via built-in speaker
one
Receiving an audio route gives an empty string
one
iPhone - AVAudioPlayer, kAudioSessionCategory_AmbientSound and iPod music
one
plug-in for connecting headphones when the audio route does not change - iOS
0
MPMusicPlayerController kills RemoteIO on iPhone

All Articles