算法或者代码iOS 系列iOS技术专题

voip APP切换听筒和扬声器以及距离传感器

2016-05-09  本文已影响319人  北海有鱼w

在项目中需要切换听筒和扬声器模式,碰到一点小问题,查看了一些资料,在此记录一下。
距离传感器
在需要的地方:

#pragma mark --设置距离传感器
- (void)setproximity{
    //添加近距离事件监听,添加前先设置为YES,如果设置完后还是NO的读话,说明当前设备没有近距离传感器
    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
    
    if ([UIDevice currentDevice].proximityMonitoringEnabled) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
    }
}
//proximityState 属性 如果用户接近手机,此时属性值为YES,并且屏幕关闭(非休眠)。
-(void)sensorStateChange:(NSNotificationCenter *)notification{
    if ([[UIDevice currentDevice] proximityState]) {
        NSLog(@"Device is close to user");
        //设置AVAudioSession 的播放模式
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    }else{
        NSLog(@"Device is not close to user");
    }
}

删除监听

- (void)dealloc{
    if ([UIDevice currentDevice].proximityMonitoringEnabled) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
    }
    [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
}

关于扬声器和听筒模式的切换
在iOS7之后,可以设置AVAudioSession 的 setCategory 类别来进行语音的一些模式,常用设置如下:

#pragma mark -- Values for the category property --

/*  Use this category for background sounds such as rain, car engine noise, etc.  
 Mixes with other music. */
AVF_EXPORT NSString *const AVAudioSessionCategoryAmbient;
    
/*  Use this category for background sounds.  Other music will stop playing. */
AVF_EXPORT NSString *const AVAudioSessionCategorySoloAmbient;

/* Use this category for music tracks.*/
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayback;

/*  Use this category when recording audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;

/*  Use this category when recording and playing back audio. */
AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;

就是在这里碰到了问题,在切换扬声器的时候我设置了:

 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];

它确实可以切换到扬声器播放,但是却不能录音,也就是说我们只能听到从对方手机传过来的声音,自己说话的声音却传不到对方手机,看下它的注释/* Use this category for music tracks.*/它好像只能用于音乐或者一些别的语音的输出,而我们的需求是它既要有输出也要可以输入,就是录音功能,在网上查找了一些资料,终于找到了解决方法:

  1. 设置AVAudioSession 的 Category
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

/* Use this category when recording and playing back audio. */ AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndRecord;在录制和播放时使用此类。

  1. 当我们切换扬声器时,只需要这样设置
[[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
  1. 完整代码
-(void)receive:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (!sender.selected) {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    }else{
        [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
        }
}

这样就可以进行扬声器播放并录制声音了。

上一篇下一篇

猜你喜欢

热点阅读