您的位置:首页 > 移动开发 > IOS开发

iOS 切换听筒与扬声器

2018-01-24 18:14 1466 查看
/** 1.先设置为外放 */
dispatch_async(dispatch_get_main_queue(), ^{
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
});
/** 2.判断当前的输出源 */
[self routeChange:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(routeChange:)
name:AVAudioSessionRouteChangeNotification
object:[AVAudioSession sharedInstance]];

 
- (void)routeChange:(NSNotification*)notify{
if(notify){
NSLog(@"声音声道改变%@",notify);
}
AVAudioSessionRouteDescription*route = [[AVAudioSession sharedInstance]currentRoute];
for (AVAudioSessionPortDescription * desc in [route outputs]) {
NSLog(@"当前声道%@",[desc portType]);
NSLog(@"输出源名称%@",[desc portName]);
if ([[desc portType] isEqualToString:@"Headphones"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
});
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息