irpas技术客

iOS 关于蓝牙耳机和录音录制视频的设置_魏先生wzp_ios开发 播放音乐 蓝牙耳机

未知 6653

1.首先你得先检测是否是蓝牙耳机接入

- (NSInteger)HeadSetPluggingType {

? ? AVAudioSession *audioSession = [AVAudioSession sharedInstance];

? ? ? ? AVAudioSessionRouteDescription *currentRoute = [audioSession currentRoute];

? ? for (AVAudioSessionPortDescription *output in currentRoute.outputs) {

? ? ? ? if ([[output portType] isEqualToString:AVAudioSessionPortHeadphones]) {

? ? ? ? ? ? return 1;

? ? ? ? }else if ([[output portType] isEqualToString:AVAudioSessionPortBluetoothA2DP]) {

? ? ? ? ? ? return 2;

? ? ? ? }

? ? }

? ? return 0;

}

我这边返回的1是有线耳机,2是蓝牙设备,3没有接入设备

2.耳机状态获取的通知

[[NSNotificationCenter defaultCenter]

?? ? ? ? ? ? addObserver:self

? ? ? ? ? ? ? ? selector:@selector(audioRouteChangeListenerCallback:)

? ? ? ? ? ? ? ? ? ? name:AVAudioSessionRouteChangeNotification

? ? ? ? ? ? ? ? ? object:[AVAudioSession sharedInstance]];

//监听耳机插入拔出状态的改变

- (void)audioRouteChangeListenerCallback:(NSNotification *)notification {

? ? NSDictionary *interuptionDict = notification.userInfo;

? ? NSInteger routeChangeReason ? = [[interuptionDict

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? valueForKey:AVAudioSessionRouteChangeReasonKey]? ? ? ? integerValue];

? ? switch (routeChangeReason) {

? ? ? ? case AVAudioSessionRouteChangeReasonNewDeviceAvailable:{//插入耳机

? ? ? ? ? ? NSLog(@"eeejjejejejjejjjejej-插入耳机");

?? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? }

? ? ? ? ? ? break;

? ? ? ? case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:{//拔出耳机

? ? ? ? ? ? NSLog(@"eeejjejejejjejjjejej-拔出耳机");

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? }

? ? ? ? ? ? break;

? ? ? ? case AVAudioSessionRouteChangeReasonCategoryChange:

? ? ? ? ? ??

? ? ? ? ? ? break;

? ? }

}

3.AVAudioSession 设置

一.录制视频 ?需要佩戴蓝牙耳机的

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVideoRecording options:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

? ? ? ? ? ? ? ? ? ? ? ? [[AVAudioSession sharedInstance] setActive:YES error:nil];

二.播放视频且需要蓝牙耳机录音的

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

? ? ? ? ? ? [[AVAudioSession sharedInstance] setActive:YES error:nil];


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #ios开发 #播放音乐 #蓝牙耳机 #iOS