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]; } } } }
ios avfoundation audio avplayer audiotoolbox
user1530580
source share