博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS__播放mp3文件根据光线传感器设置外置或者听筒播放
阅读量:7197 次
发布时间:2019-06-29

本文共 2116 字,大约阅读时间需要 7 分钟。

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];NSError *playerError;  //    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:[VoiceRecorderBaseVC getPathByFileName:recordedFile ofType:@"aac"]] error:&playerError];self.player.meteringEnabled = YES;if (self.player == nil){    NSLog(@"ERror creating player: %@", [playerError description]);}[self handleNotification:YES];self.player.delegate = self;self.player.volume = 1;[self.player play];复制代码

#pragma mark - 监听听筒or扬声器 - (void) handleNotification:(BOOL)state { [[UIDevice currentDevice] setProximityMonitoringEnabled:state]; //建议在播放之前设置yes,播放结束设置NO,这个功能是开启红外感应

if(state)//添加监听    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification"                                               object:nil];else//移除监听    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIDeviceProximityStateDidChangeNotification" object:nil];}复制代码

//处理监听触发事件

-(void)sensorStateChange:(NSNotificationCenter *)notification;{  //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)    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];    }}复制代码

转载地址:http://bzakm.baihongyu.com/

你可能感兴趣的文章
CSS3中的动画效果-------Day72
查看>>
Codeforces 97B Superset 平面分治
查看>>
Android studio在ubuntu下安装【转】
查看>>
adb的那点小事——360电视助手实现研究
查看>>
源码浅谈(一):java中的 toString()方法
查看>>
ionic中关于ionicView 的生命周期
查看>>
iOS_4_表情排列
查看>>
netsh http的使用
查看>>
Xcode常见问题
查看>>
【管理学习】团队领导力之辅导
查看>>
使用Linq 查询数据 构建对象 select new{}
查看>>
第二百六十八节,Tornado框架-路由映射之二级域名支持,html模板继承以及导入...
查看>>
CentOS6.x修改主机名,关闭防火墙
查看>>
Java中static作用及使用方法具体解释
查看>>
SVN版本回退
查看>>
HTML5的Video标签的属性,方法和事件汇总
查看>>
Kids Store - OpenCart 自适应主题模板 ABC-0022
查看>>
Swift开发教程--怎样自己定义TabBarItem的图片显示
查看>>
Cacti学习
查看>>
ASP.NET Core 返回 Json DateTime 格式
查看>>