iOS下微信语音播放之切换听筒和扬声器的方法解决方案
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO,这个功能是开启红外感应
//添加监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:)
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];
}
}
//初始化播放器的时候如下设置
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 的距离传感器。
使用近距离传感器
UIDevice 中有两个近距离传感器的属性: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:NO];
[[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 *)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];
if (![MTool isPlayRecodering]) {//没有播放了,也没有在黑屏状态下,就可以把距离传感器关了
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
}
}
}
iOS下微信语音播放之切换听筒和扬声器的方法解决方案的更多相关文章
- iOS 类微信语音播放之切换听筒和扬声器的方法解决方案
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; //建议在播放之前设置yes,播放结束设置NO,这个功能是 //添加监听 ...
- iOS语音播放之切换听筒和扬声器的方法解决方案
关于流媒体播放的相关知识可以加本人QQ:564702640 一起来讨论 [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; // ...
- 微信h5页面audio标签在ios下不能自动播放
背景介绍:在一个h5页面中,当用户提交表单到后台,后台返回的结果成功的话,开始自动播放背景音乐 出现的问题:在安卓手机上正常,iOS中没有反应 后来网上一番搜索后了解到时因为iOS不允许自动播放音乐, ...
- html5的audio实现高仿微信语音播放效果
效果图 前台大体呈现效果图如下: 点击就可以播放mp3格式的录音.点击另外一个录音,当前录音停止! 思路 关于播放动画,这个很简单,我们可以用css3的逐帧动画来实现.关于逐帧动画,我之前的文章也写过 ...
- js模仿微信语音播放的小功能
自己写的一个模仿微信语音播放的小功能,实现的主要功能是:点击播放,点击暂停,播放切换,,, 代码如下: <!DOCTYPE html> <html lang="en&qu ...
- 纯css3配合vue实现微信语音播放效果
前言 每次写点东西都扯两句-0-,这几天一半精力放在移动端,一半维护之前的项目.书也少看了,不过还好依旧保持一颗学习的心.对于css3我是之前有专门整理过的,因此对于原理之前也算了解.今天是项目中遇到 ...
- ios下微信标题修改
很多开发过微信的人估计都遇到过这样的问题,ios下微信页面标题更改不了,而安卓却可以直接用:document.title="你的标题". 下面是解决这个问题的hack: 1.jqu ...
- ios下微信浏览器如何唤醒app?app已上架应用宝
android下可以通过在应用宝微下载地址后面加参数&android_schema='应用schema'来实现,ios下如何实现? ios下微信浏览器如何唤醒app?app已上架应用宝 > ...
- js仿微信语音播放
html结构如下: <div class="app-voice-you" voiceSrc="xx.mp3"> <img class=&quo ...
随机推荐
- 纯web实现游记类手机端应用
初衷 当初的一个学习框架项目,采用sui框架实现的一套手机端页面.今天清理github的时候重新整理了一下,因为设计的确实不错嘛,拿出来大家一起学习...哈哈 说明 采用sui框架 纯html/css ...
- fuzz实战之honggfuzz
Honggfuzz实战 前言 本文介绍 libfuzzer 和 afl 联合增强版 honggfuzz .同时介绍利用 honggfuzz 来 fuzz 网络应用服务. 介绍 honggfuzz 也是 ...
- Android自定义Button按钮显示样式
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- 【Java】操作Sqlite数据库
首先在https://github.com/xerial/sqlite-jdbc下载jar包 import java.sql.Connection; import java.sql.DriverMan ...
- 使用cnpm install提示package not found
使用cnpm install 的时候提示Can't find package. 在cnpm package里能搜索到scratch-audio的包,但版本是0.1.0-prerelease.20190 ...
- Mybatis将结果放入map时别名不是驼峰形式
查询时如果给字段起别名,并且将查询结果映射到一个Map,那么Map的key将是忽略大小写的.映射到一个实体类是没这个问题的. state as addState 从Map中取值时应该:map.get( ...
- Windows Pre-commit hook for comment length Subversion
@echo off :: Stops commits that have empty log messages. @echo off setlocal rem Subversion sends thr ...
- maskView遮罩中多张图片的动画
maskView遮罩中多张图片的动画 说明 用多张图片做遮罩效率极高,非常好理解,而且极其美观! 效果图 素材 源码 // // ViewController.m // FeedBack // // ...
- Linux ifconfig命令详解
ifconfig(interfaces config).通常需要以root身份登录或使用sudo来使用ifconfig工具 ifconfig 命令用来查看和配置网络设备.当网络环境发生改变时可通过此命 ...
- Linux配置自动发送邮件
需要的工具:sendEmail 和 linux自带的定时工具:crontab 1.sendEmail的使用: 具体参数解释: -f zhangshibo706@163.com 发件人邮箱 -t 453 ...