oppor5微信自动切换听筒模式式切换扬声器怎么弄

CoreAudio实现录音播音和扬声器听筒模式的切换 - 推酷
CoreAudio实现录音播音和扬声器听筒模式的切换
本例子使用Core Audio实现类似于微信的音频对讲功能,可以录音和播放并且实现了听筒模式和扬声器模式的切换。录音主要使用AVAudioRecorder类来实现录音功能,播放则使用AVAudioPlayer类来实现播放音频功能,扬声器和听筒模式的切换是通过设置AVAudioSession类的属性值来实现切换效果。
- (void)viewDidLoad
&&& [super viewDidLoad];
//创建录音按钮
&& &UIButton* recorderB = [[[UIButton alloc]initWithFrame:CGRectMake(20, 220, 60, 40)]autorelease];
&&& [recorderB setTitle:@&record& forState:UIControlStateNormal];
&&& [recorderB setBackgroundColor:[UIColor grayColor]];
&&& [recorderB addTarget:self action:@selector(prerecord:) forControlEvents:UIControlEventTouchDown];
&&& [recorderB addTarget:self action:@selector(startrecord:) forControlEvents:UIControlEventTouchUpInside];
&&& [self.view addSubview:recorderB];
//创建播放按钮
&&& UIButton* play = [[[UIButton alloc]initWithFrame:CGRectMake(230, 220, 60, 40)]autorelease];
&&& [play setTitle:@&play& forState:UIControlStateNormal];
&&& [play setBackgroundColor:[UIColor grayColor]];
&&& [play addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
&&& [self.view addSubview:play];
//创建听筒模式和扬声器模式切换按钮
&&& change = [[[UISwitch alloc]initWithFrame:CGRectMake(120, 225, 40, 40)]autorelease];
//默认设置为没有打开,则用听筒模式
&&& [change setOn:NO];
&&& [change addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
&&& [self.view addSubview:change];
//切换按钮事件
-(void)change:(UISwitch*)sender{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
&&& AVAudioSession *audioSession = [AVAudioSession sharedInstance];
&&& if ([sender isOn]) {
//设置下扬声器模式
&&&&&&& [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
&&& }else{
//设置听筒模式
&&&&&&& [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
&&& [audioSession setActive:YES error:nil];
//录音事件
-(void)record:(id)sender{
//获取当前应用的AudioSession,每个应用都有自己唯一的声频会话
&&&&&&& AVAudioSession * audioSession = [AVAudioSession sharedInstance];
&&&&&&& NSError* error1;
&&& //设置当前应用的声频会话为录音模式
&&&&&&& /*
&&&&&&& Use this category for background sounds such as rain, car engine noise, etc.
&&&&&&&& Mixes with other music.
&&&&&&&& AVF_EXPORT NSString *const AVAudioSessionCategoryA
&&&&&&&& Use this category for background sounds.& Other music will stop playing.&&&& AVF_EXPORT NSString *const AVAudioSessionCategorySoloA
&&&&&&&& Use this category for music tracks.
&&&&&&&& AVF_EXPORT NSString *const AVAudioSessionCategoryP
&&&&&&&& Use this category when recording audio.
&&&&&&&& AVF_EXPORT NSString *const AVAudioSessionCategoryR
&&&&&&&& Use this category when recording and playing back audio.
&&&&&&&& AVF_EXPORT NSString *const AVAudioSessionCategoryPlayAndR
&&&&&&&& Use this category when using a hardware codec or signal processor while
&&&&&&&& not playing or recording audio.
&&&&&&&& AVF_EXPORT NSString *const AVAudioSessionCategoryAudioP
&&&&&&&& */
&&&&&&& [audioSession setCategory:AVAudioSessionCategoryRecord error: &error1];
//激活音频会话
&&&&&&& [audioSession setActive:YES error: &error1];
&&&&&&& NSLog(@&record...&);
//创建录音的文件保存位置
&&&&&&& NSString* filepath = [DOCUMENT_PATH stringByAppendingPathComponent:@&test.caf&];
&&&&&&& url = [[NSURL fileURLWithPath:filepath]retain];
&&&&&&& NSLog(@&path:%@&,filepath);
//设置录音的相关属性
&&&&&&& NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
//设置音频格式
&&&&&&& [settings
&&&&&&&& setValue:[NSNumber numberWithInteger:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//设置采用率
&&&&&&& [settings
&&&&&&&& setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey];
//设置频道
&&&&&&& [settings
&&&&&&&& setValue:[NSNumber numberWithInteger:2] forKey:AVNumberOfChannelsKey];
//设置录音质量
&&&&&&& [settings
&&&&&&&& setValue:[NSNumber numberWithInteger:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
&&&&&&& NSError*
&&&&&&& recorder = [[AVAudioRecorder alloc]initWithURL:url settings:settings error:&error];
&&&&&&& recorder.delegate =
//录音准备,会为建立录音文件做好需要的准备
&&&&&&& BOOL isrecord = [recorder prepareToRecord];
&&&&&&& NSLog(@&isrecord:%d&,isrecord);
//正式开始录音
&&&&&&& [recorder record];
&&&&&&& NSLog(@&record begin...&);
//手指离开点击录音按钮的事件
-(void)startrecord:(id)sender{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
&&& NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
//获取长按录音按钮的时间
&&& NSTimeInterval offset = now -
&&& NSLog(@&offset:%f&,offset);
//如果长按的时间少于1秒,则不录音
&&& if (offset&=1) {
&&&&&&& [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(record:) object:self];
&&& }else{
//超过一秒,则完成录音,停止继续录音
&&&&&&& [self stoprecorder:recorder];
//手指点击录音按钮时间
-(void)prerecord:(id)sender{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
//获取点击的时间
&&& starttime = [[NSDate date]timeIntervalSince1970];
&&& NSLog(@&starttime:%f&,starttime);
//点击超过一秒后才正式录音
&&& [self performSelector:@selector(record:) withObject:self afterDelay:1];
//停止录音的事件
-(void)stoprecorder:(AVAudioRecorder*)sender{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
//判断是否还在录音,如果是就停止
&&& if ([recorder isRecording]) {
&&&&&&& [recorder stop];
//播放事件
-(void)play:(id)sender{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
&&& NSError*
&&& NSLog(@&url:%@&,url);
//通过AVAudioPlayer打开刚才录音的文件
&&& AVAudioPlayer* player = [[AVAudioPlayer alloc]initWithData:[NSData dataWithContentsOfURL:url] error:&error];
//AVAudioPlayer预处理,为需要播放做好准备
&&& BOOL isplay = [player prepareToPlay];
&&& NSLog(@&isplay:%d&,isplay);
& //& player.volume = 1;
&&& player.delegate =
//正式播放
&&& [player play];
- (void)didReceiveMemoryWarning
&&& [super didReceiveMemoryWarning];
&&& // Dispose of any resources that can be recreated.
//录音完成后的回调事件
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
//录音完成后关闭释放资源
&&& if ([recorder isRecording]) {
&&&&&&& [recorder stop];
&&& [recorder release];
//录音的时候因为设置的音频会话是录音模式,所以录音完成后要把音频会话设置回听筒模式或者扬声器模式,根据切换器的值判断
&&& AVAudioSession *audioSession = [AVAudioSession sharedInstance];
&&& if ([change isOn]) {
//扬声器模式
&&&&&&& [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
&&& }else{
//听筒模式
&&&&&&& [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
&&& [audioSession setActive:YES error:nil];
//录音编码出错的回调
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error{
&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
&&& [recorder release];
//播放音频完成后的回调
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
&&&& NSLog(@&%@&,NSStringFromSelector(_cmd));
//成功播放完音频后释放资源
&&& if ([player isPlaying]) {
&&&&&&& [player stop];
&&& [player release];
//音频播放解码出错的回调
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
&NSLog(@&%@&,NSStringFromSelector(_cmd));
&&& [player release];
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致19237人阅读
IPhone开发中级系列(85)
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];&//建议在播放之前设置yes,播放结束设置NO,这个功能是开启红外感应
//添加监听
[[NSNotificationCenter defaultCenter] addObserver:self
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&selector:@selector(sensorStateChange:)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&name:@&UIDeviceProximityStateDidChangeNotification&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&object:nil];
//处理监听触发事件
-(void)sensorStateChange:(NSNotificationCenter&*)
&&&&//如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)
&&&&if&([[UIDevice&currentDevice]&proximityState]
&&&&&&&&NSLog(@&Device
is close to user&);
&&&&&&&&[[AVAudioSession&sharedInstance]&setCategory:AVAudioSessionCategoryPlayAndRecord&error:nil];
&&&&&&&&NSLog(@&Device
is not close to user&);
&&&&&&&&[[AVAudioSession&sharedInstance]&setCategory:AVAudioSessionCategoryPlayback&error:nil];
//初始化播放器的时候如下设置
UInt32&sessionCategory =&kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
&&&&&&&&&&&&&&&&&&&&&&&&sizeof(sessionCategory),
&&&&&&&&&&&&&&&&&&&&&&&&&sessionCategory);
UInt32&audioRouteOverride
=&kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
&&&&&&&&&&&&&&&&&&&&&&&&&sizeof&(audioRouteOverride),
&&&&&&&&&&&&&&&&&&&&&&&&&&audioRouteOverride);
AVAudioSession&*audioSession = [AVAudioSession&sharedInstance];
//默认情况下扬声器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES&error:nil];
--------------------看什么看,这是分割线。再看把你也割了--------------------------------
这里是新的补充,因为上面的做法在实际应用中可能会有一些问题,以前只是看了一下,没有应用到实际的项目中。
在 iOS 中,并非所有 iOS 设备都拥有近距离传感器。这里介绍如何调用 iPhone 的距离传感器。
使用近距离传感器
&中有两个近距离传感器的属性:proximityMonitoringEnabled 和 proximityState。这两个属性都是 iOS 3.0 及以上才支持的。
proximityMonitoringEnabled 属性
To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.
要确定近距离传感器是否可用,可以尝试启用它,即 proximityMonitoringEnabled = YES,如果设置的属性值仍然为NO,说明传感器不可用。
proximityState 属性
传感器已启动前提条件下,如果用户接近 近距离传感器,此时属性值为YES,并且屏幕已关闭(非休眠)。And vice versa。
Notification
UIDeviceProximityStateDidChangeNotification,当近距离传感器状态改变时发生。
&&&&//添加近距离事件监听,添加前先设置为YES,如果设置完后还是NO的读话,说明当前设备没有近距离传感器
&&&&[[UIDevice&currentDevice]&setProximityMonitoringEnabled:YES];
&&&&if&([UIDevice&currentDevice].proximityMonitoringEnabled&==&YES)
&&&&&&&&[[NSNotificationCenter&defaultCenter]&addObserver:self&selector:@selector(sensorStateChange:)name:UIDeviceProximityStateDidChangeNotification&object:nil];
//删除近距离事件监听
&&&&[[UIDevice&currentDevice]&setProximityMonitoringEnabled:YES];
&&&&if&([UIDevice&currentDevice].proximityMonitoringEnabled&==&YES)
&&&&&&&&[[NSNotificationCenter&defaultCenter]&removeObserver:self&name:UIDeviceProximityStateDidChangeNotification&object:nil];
&&&&[[UIDevice&currentDevice]&setProximityMonitoringEnabled:NO];
#pragma mark -&处理近距离监听触发事件
-(void)sensorStateChange:(NSNotificationCenter&*)
&&&&//如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)
&&&&if&([[UIDevice&currentDevice]&proximityState]
==&YES)//黑屏
&&&&&&&&NSLog(@&Device
is close to user&);
&&&&&&&&[[AVAudioSession&sharedInstance]&setCategory:AVAudioSessionCategoryPlayAndRecord&error:nil];
&&&&else//没黑屏幕
&&&&&&&&NSLog(@&Device
is not close to user&);
&&&&&&&&[[AVAudioSession&sharedInstance]&setCategory:AVAudioSessionCategoryPlayback&error:nil];
&&&&&&&&if&(![MTool&isPlayRecodering])
{//没有播放了,也没有在黑屏状态下,就可以把距离传感器关了
&&&&&&&&&&&&[[UIDevice&currentDevice]&setProximityMonitoringEnabled:NO];
注意事项(也就是我说的问题)
&&&&对于不希望启动接近传感器功能的应用,如果需要进行扬声器和听筒进行切换过程中,则必须通过启用接近传感器来进行声音输出模式的切换,在此时,必须要注意,如果当声音通过听筒进行播放完毕时,在播放完毕时,此时仍在听筒模式输出,如果此时关闭传感器功能,则导致在离开听筒时,由于传感器功能已经关闭,应用无法再次收到注册的传感器变更通知,而此时如果未能将底层的声音输出模式切换,则导致相关的声音输出仍从听筒中输出,即使引起传感器反映的障碍已经离开传感器作用范围,但应用中获取的传感器状态仍未接近状态,使根据传感器状态进行切换声音输出模式操作失效。&
&&&&特殊情况:
在iPhone 4s及iPhone5中,在接近传感器功能关闭后,如果此时传感器状态为YES,则在再次启动声音传感器时,不会收到传感器的变更通知;
在iPhone 4中,在接近传感器功能关闭后,如果此时传感器状态为YES,则在再次启动声音传感器时,会先收到一次传感器的变更通知;
&&&此问题的解决方案:当在传感器功能开始时,如果此时传感器传感状态为YES时,此时声音播放结束,仍未出发传感器状态变更时,此时不关闭传感器功能。当引起传感器反映的障碍已经离开传感器作用范围,此时会收到传感器变更通知,在变更通知中检测当前传感器状态是否为开启状态及声音播放状态,如果在传感器状态为YES时,而此时需要开启传感器功能的操作(如声音播放功能)已经结束时,则将传感器功能关闭即可;
-------也就是说,在不是黑屏的状态下,关闭近传感器功能。就没什么问题了。
手动切换两种模式
解决方案:添加长按手势,切换为另一种模式。
代码片段:
UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer
initWithTarget:self
action:@selector(longPressed:)];
& & [longPressGestureRecognizer
setMinimumPressDuration:1.0f];
& & [longPressGestureRecognizer
setAllowableMovement:50.0];
& & [self.bubbleBgImageView
addGestureRecognizer:longPressGestureRecognizer];
& & [longPressGestureRecognizer
---------
-(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer
switch (gestureRecognizer.state)
& & & & case
UIGestureRecognizerStateEnded:
&& & & & & &
& & & & & &
& & & & case
UIGestureRecognizerStateCancelled:
&& & & & & &
& & & & & &
& & & & case
UIGestureRecognizerStateFailed:
&& & & & & &
& & & & & &
& & & & case
UIGestureRecognizerStateBegan:
& & & & & &
if ([self.voiceDelegate
respondsToSelector:@selector(BaseChartVoiceLongPressed)])
& & & & & & {
& & & & & & & & [self.voiceDelegate
BaseChartVoiceLongPressed];
& & & & & & }
& & & & & &
& & & & case
UIGestureRecognizerStateChanged:
&& & & & & &
& & & & & &
& & & & & &
-------------
#pragma mark BaseChartCellDelegate
-(void)BaseChartVoiceLongPressed
& & NSLog(@&voice long Pressed&);
& & if ([[[AVAudioSession
sharedInstance]
isEqualToString:AVAudioSessionCategoryPlayback])
& & & & //切换为听筒播放
& & & & [[AVAudioSession
sharedInstance]
setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];
& & & & [self
showTipInfo:@&切换为听筒模式&];
& & & & //切换为扬声器播放
& & & & [[AVAudioSession
sharedInstance]
setCategory:AVAudioSessionCategoryPlayback
error:nil];
& & & & [self
showTipInfo:@&切换为扬声器模式&];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1054477次
积分:11711
积分:11711
排名:第784名
原创:186篇
转载:101篇
评论:256条
(2)(2)(6)(2)(3)(4)(2)(2)(1)(5)(12)(5)(4)(2)(3)(8)(12)(7)(23)(3)(8)(7)(21)(6)(10)(40)(19)(3)(12)(17)(1)(1)(13)(4)(10)(2)(6)(1)(1)iphone5无缘无故扬声器不能外放.只能用听筒模式.
内容为广告/垃圾,我要举报!
特聘专家具有协助内容审核的特权
举报后内容将不能在前台展示
错乱举报会导致该权利被剥夺
选择举报原因&
切换到扬声器状态没有声音.卸载了再装微信也没有声音.关机了再开机仍然没有声音.有谁知道为什么吗?我的是6.1.3的版本.
已有1个回答
[编辑专家]
专家星级&:&0.5星
问答堂专家综合评分
问题评分&:&0星
采纳、点赞&:&0星
二次回复率&:&5星
内容为广告/垃圾,我要举报!
特聘专家具有协助内容审核的特权
举报后内容将不能在前台展示
错乱举报会导致该权利被剥夺
选择举报原因×
是硬件问题,建议拿到苹果售后检测,质保范围内可以换新机。
留下你的评论
该内容由系统自动生成
参考价:¥3800.00
主屏尺寸4英寸
电池容量1440mAh
主屏分辨率像素
电池类型不可拆卸式电池
后置摄像头800万像素
CPU频率1.0GHz
前置摄像头120万像素
CPU类型苹果 A6
网络类型单卡
名网友点评分数
本人学生狗没多少钱 当然想买便宜又原封的了 省一点是一点 港版翻新太多 同学说X宝排名前几的4200港版都是翻新的V版也买不起 哥们说我可以买个英版 便宜 解锁后一...
微信公众账号ZOL问答堂
关注微信,随时随地解答您的疑惑
ZOL问答堂官方微博@ZOL问答堂
关注成功!该问题被回答后,将给您发送站内短信。
您也可以通过关注问答堂微信,及时获得您关注问题的回答。
微信关注问题方法“”微信5.2怎么切换听筒模式_iTunes之家
微信5.2怎么切换听筒模式
时间:日 | 关键字: | 来源:
&  登陆微信后点击&我&--&设置&--&通用&。  在&通用&界面可以看到&听筒模式&切换按钮,灰色表示微信没有开启听筒模式哦~(微信5.2默认使用扬声器播放语音)

我要回帖

更多关于 微信听筒模式怎么切换 的文章

 

随机推荐