Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

ios - Unable to switch to speaker output when bluetooth headsets are connected

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...