gpt4 book ai didi

ios - 删除 AVAudioSessionPortBuiltInMic 作为选项

转载 作者:行者123 更新时间:2023-11-29 10:36:12 25 4
gpt4 key购买 nike

作为一个选项,我想删除 iPhone 的内置麦克风。虽然只有耳机出来的那个。基本上我想强制输出到耳机/蓝牙(如果可用)或主扬声器。 Apple 没有记录删除端口的方法。是否可以拦截路由更改,如果他们选择麦克风,则强制将其发送到扬声器?

最佳答案

是的,可以拦截路线更改。您需要观察 AVAudioSessionRouteChangeNotification 通知:

    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioRouteDidChanged:)
name:AVAudioSessionRouteChangeNotification
object:nil];

然后你就可以区分路线变更类型了:

- (void)audioRouteDidChanged:(NSNotification *)notification
{
// Here you can figure out the why audio route has been changed (if you need it)
AVAudioSessionRouteChangeReason reason = [notification.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey];
switch (reason)
{
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
NSLog(@"A user action (such as plugging in a headset) has made a preferred audio route available.");
break;

case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@"The previous audio output path is no longer available.");
break;

default:
NSLog(@"Other reasons. See documentation");
break;
}

// And then make a decision to disable earpiece(aka receiver).
// You can request current audio route description
AVAudioSessionRouteDescription *routeDescription = [AVAudioSession sharedInstance].currentRoute;
// then traverse through outputs and figure out if headset is present
__block BOOL headsetExists = NO;
[routeDescription.outputs enumerateObjectsUsingBlock:^(AVAudioSessionPortDescription *obj, NSUInteger idx, BOOL *stop) {
if ([obj.portType isEqualToString:AVAudioSessionPortHeadphones])
{
headsetExists = YES;
return ;
}
}];

if (headsetExists == NO)
{
// force sound to speaker
NSError *err = nil;
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&err];
}
}

关于ios - 删除 AVAudioSessionPortBuiltInMic 作为选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com