Unable to switch to speaker output when bluetooth headsets are connected

余生颓废 提交于 2019-11-27 06:32:26

问题


I'm trying to allow for a toggle between bluetooth headsets (airpods in my case) and the phone speaker, using AVAudioSession. I initialize my session as so:

AVAudioSessionCategoryOptions options = (AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker);
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:options error:&error];

Then I try to alternate between output modes as so:

-(void)setIncomingSoundMode:(IncomingSoundMode)incomingSoundMode{
    [self removeAudioRouteChangedObserver];


    [NNLogger logFromInstance: self message: @"Audio stream setting use speaker" data: @(incomingSoundMode)];
    _incomingSoundMode = incomingSoundMode;


    AVAudioSession *session = [AVAudioSession sharedInstance];
    AVAudioSessionPortDescription *routePort = session.currentRoute.outputs.firstObject;
    NSString *portType = routePort.portType;
    NSLog(@"current port type: %@",portType);


    NSError *audioPortError = nil;
    if(incomingSoundMode == IncomingSoundModeSpeaker){
        [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioPortError];
        [self muteChannel:NO];
    } else if(incomingSoundMode == IncomingSoundModeHeadset){
        [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&audioPortError];
        [self muteChannel:NO];
    } else if(incomingSoundMode == IncomingSoundModeBluetooth){
        [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&audioPortError];
        [self muteChannel:NO];
    } else if(incomingSoundMode == IncomingSoundModeSilent){
        [self muteChannel:YES];
    }

    if(audioPortError){
        NSLog(@"audioPortError - %@",audioPortError.localizedDescription);
    }
    NSError *sessionError = nil;
    [session setActive: YES error:&sessionError];
    if(sessionError){
        NSLog(@"sessionError - %@",sessionError.localizedDescription);
    }

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self addAudioRouteChangedObserver];
    });

}

The issue is when I try to override to speaker output while bluetooth headphones are connected - it simply doesn't switch to the speaker. This same functionality works with wired headphones, or when toggling device speaker to headset:

[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&audioPortError];
[session setActive: YES error:&sessionError];

Any clues on what I'm doing wrong here??

Thanks


回答1:


I have exact same issue in the application I'm working on and it's reproducible only on iOS 12+ and only for W1 / H1 chip bluetooth devices (AirPods / new Beats Studio, etc.).

I asked apple audio engineers on WWDC about it (by showing my bug report) and they have confirmed that this is a known issue they're working on. Should be fixed in iOS update.. somewhen later :) To speed things up, you could duplicate this bug report: rdar://49734534

P.S. There is no pretty workaround for this issue, but there are few options:

  1. Use MPVolumeView's route button. It works, and seems like this is how WhatsApp handles this case.
  2. Disable bluetooth option for AVAudioSession. Far from perfect solution, but may work for some apps.


来源:https://stackoverflow.com/questions/53922387/unable-to-switch-to-speaker-output-when-bluetooth-headsets-are-connected

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