关于流媒体播放的相关知识可以加本人QQ:564702640 一起来讨论

[[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: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];

}

}

}

 
 
 
注意事项(也就是我说的问题)
    对于不希望启动接近传感器功能的应用,如果需要进行扬声器和听筒进行切换过程中,则必须通过启用接近传感器来进行声音输出模式的切换,在此时,必须要注意,如果当声音通过听筒进行播放完毕时,在播放完毕时,此时仍在听筒模式输出,如果此时关闭传感器功能,则导致在离开听筒时,由于传感器功能已经关闭,应用无法再次收到注册的传感器变更通知,而此时如果未能将底层的声音输出模式切换,则导致相关的声音输出仍从听筒中输出,即使引起传感器反映的障碍已经离开传感器作用范围,但应用中获取的传感器状态仍未接近状态,使根据传感器状态进行切换声音输出模式操作失效。 
    特殊情况:
在iPhone 4s及iPhone5中,在接近传感器功能关闭后,如果此时传感器状态为YES,则在再次启动声音传感器时,不会收到传感器的变更通知;
在iPhone 4中,在接近传感器功能关闭后,如果此时传感器状态为YES,则在再次启动声音传感器时,会先收到一次传感器的变更通知;
   此问题的解决方案:当在传感器功能开始时,如果此时传感器传感状态为YES时,此时声音播放结束,仍未出发传感器状态变更时,此时不关闭传感器功能。当引起传感器反映的障碍已经离开传感器作用范围,此时会收到传感器变更通知,在变更通知中检测当前传感器状态是否为开启状态及声音播放状态,如果在传感器状态为YES时,而此时需要开启传感器功能的操作(如声音播放功能)已经结束时,则将传感器功能关闭即可;
 
-------也就是说,在不是黑屏的状态下,关闭近传感器功能。就没什么问题了。
 
手动切换两种模式
解决方案:添加长按手势,切换为另一种模式。
代码片段:

UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self

action:@selector(longPressed:)];

[longPressGestureRecognizer setMinimumPressDuration:1.0f];

[longPressGestureRecognizer setAllowableMovement:50.0];

[self.bubbleBgImageView addGestureRecognizer:longPressGestureRecognizer];

[longPressGestureRecognizer release];

---------

-(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer

{

switch (gestureRecognizer.state)

{

case UIGestureRecognizerStateEnded:

break;

case UIGestureRecognizerStateCancelled:

break;

case UIGestureRecognizerStateFailed:

break;

case UIGestureRecognizerStateBegan:

if ([self.voiceDelegate respondsToSelector:@selector(BaseChartVoiceLongPressed)])

{

[self.voiceDelegate BaseChartVoiceLongPressed];

}

break;

case UIGestureRecognizerStateChanged:

break;

default:

break;

}

}

 

-------------

#pragma mark BaseChartCellDelegate

-(void)BaseChartVoiceLongPressed

{

NSLog(@"voice long Pressed");

if ([[[AVAudioSession sharedInstance] category]isEqualToString:AVAudioSessionCategoryPlayback])

{

//切换为听筒播放

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

[self showTipInfo:@"切换为听筒模式"];

}

else

{

//切换为扬声器播放

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

[self showTipInfo:@"切换为扬声器模式"];

}

}

iOS语音播放之切换听筒和扬声器的方法解决方案的更多相关文章

  1. iOS下微信语音播放之切换听筒和扬声器的方法解决方案

    [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO,这个功能是开启红外感应 // ...

  2. iOS 类微信语音播放之切换听筒和扬声器的方法解决方案

    [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];   //建议在播放之前设置yes,播放结束设置NO,这个功能是 //添加监听 ...

  3. js模仿微信语音播放的小功能

    自己写的一个模仿微信语音播放的小功能,实现的主要功能是:点击播放,点击暂停,播放切换,,,  代码如下: <!DOCTYPE html> <html lang="en&qu ...

  4. iOS音频播放之AudioQueue(一):播放本地音乐

    AudioQueue简单介绍 AudioStreamer说明 AudioQueue具体解释 AudioQueue工作原理 AudioQueue主要接口 AudioQueueNewOutput Audi ...

  5. iOS OC环信实时语音切换听筒免提听不到声音报错:AVAudioSessionErrorCodeBadParam

    出现这个报错:AVAudioSessionErrorCodeBadParam 先看看你的问题是不是在切换听筒免提的时候 听不到声音了, 不是的可以继续搜索去了   问题在这里 把圈住的那个货换成这个就 ...

  6. IOS 音频播放

    iOS音频播放 (一):概述 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也因此对于iOS下的音频播放实现有了一定的研究.写这个系列的博客目的一方面希望能够抛砖 ...

  7. jQuery语音播放插件

    自己做jQuery插件:将audio5js封装成jQuery语音播放插件   日前的一个项目需要用到语音播放功能.发现Audio5js符合需求且使用简单,又鉴于jQuery控件便于开发操作,于是有了以 ...

  8. iOS音频播放(二):AudioSession

    (本文转自码农人生) 前言 在实施前一篇中所述的7个步骤步之前还必须面对一个麻烦的问题,AudioSession.   AudioSession简介 AudioSession这个玩意的主要功能包括以下 ...

  9. iOS音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

随机推荐

  1. tomcat集群部署

    1.apache只有处理静态事物的能力, 而tomcat的强项就是处理动态的请求 2.由apache作为入口,如果是请求静态页面或者是静态文件,由apache直接提供,如果是请求动态页面,则让apac ...

  2. POJ1753——Flip Game

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  3. 企业级 Linux 安全管理实例(1)

    公司企业多用Linux服务器,其中涉及到的一些安全管理对于安全运维人员来说是必不可少的应知技能, 以下案例沿着背景->需求->具体要求->操作步骤的流程进行描述,可以加深对安全管理的 ...

  4. java截取url中的值

    Map<String, Object> urlSplit(String data){ StringBuffer strbuf = new StringBuffer(); StringBuf ...

  5. Ubuntu 12.04搭建Andorid编译环境

    1.安装JDK,Android 5.0开始,开始使用OpenJDK 1.7,4.4等低版本是Oracke JDK1.6 install java environment // install open ...

  6. Miles per gallon to kilometers per liter

    Miles per gallon to kilometers per liter 1 Imperial Gallon = 4.54609188 litres 1 Mile = 1.609344 kil ...

  7. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...

  8. Vs 引用第三方DLL文件 版本不一致问题 (npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案)

    最近在 做 MailChimp 与网站功能 集成时,发现 MailChimp 2API 中的 MailChimp.dll  中的依赖项 SerivceStack.Text.dll (版本为3.9.71 ...

  9. 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  10. Spring Autowire自动装配介绍

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...