Switching between headphone & speaker on iPhone

ぃ、小莉子 提交于 2019-12-11 04:42:19

问题


I am trying to set up the audio routing for an iPhone app outputting. I am using a route change listener to detect when the audio route has changed. The listener detects the changes, such as when the headphones are plugged in and out. By default, the speaker plays audio and then I plug my headphones in and the audio transmits through the headphones fine. From there, any changes do not occur, even though the route change listener is detecting 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 listener:

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];
            }
        }
    }
}

来源:https://stackoverflow.com/questions/15128455/switching-between-headphone-speaker-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!