我想删除内置麦克风的iPhone作为选项。只有耳机出来的那个。基本上我想强制输出到耳机/蓝牙(如果可用)或主扬声器。 Apple没有记录删除端口的方法。是否有可能拦截路线变化,如果他们选择麦克风强制它到扬声器?
答案 0 :(得分:2)
是的,可以拦截路线变化。您需要观察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];
}
}
答案 1 :(得分:0)
没有用于删除端口的API。但是您的应用可以检测是否以及何时插入耳机,并选择是否相应地覆盖音频路径,这将产生相同的效果。