Unable to switch to speaker output when bluetooth

2020-02-06 04:50发布

问题:

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.