本文介绍了连接蓝牙耳机时无法切换到扬声器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVAudioSession在蓝牙耳机(在我的情况下为airpods)和电话扬声器之间进行切换.我将会话初始化为:

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??

谢谢

推荐答案

我正在处理的应用程序存在完全相同的问题,并且仅在iOS 12+和W1/H1芯片蓝牙设备(AirPods/新的Beats Studio等).

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.).

我向WWDC上的苹果音频工程师询问了此问题(通过显示我的错误报告),他们确认这是他们正在解决的已知问题.应该在iOS更新中修复..稍后再出现:)为了加快速度,您可以复制此错误报告:rdar://49734534

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.对于此问题,没有很好的解决方法,但是有很少的选择:

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

  1. 使用MPVolumeView的路线按钮.它有效,并且看起来像这样WhatsApp如何处理这种情况.
  2. 禁用AVAudioSession的蓝牙选项.并非完美的解决方案,但可能适用于某些应用程序.
  1. Use MPVolumeView's route button. It works, and seems like this ishow WhatsApp handles this case.
  2. Disable bluetooth option for AVAudioSession. Far from perfect solution, but may work for some apps.

这篇关于连接蓝牙耳机时无法切换到扬声器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 21:14