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];
 
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.

 

 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:AVAudioSessionCategoryPlayAndRecord error:nil];

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

        

    }

    else

    {

        //切换为扬声器播放

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

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

    }

}

 

 

转载于:https://www.cnblogs.com/520gp-iOS/p/5264879.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS平台上,可以使用SoundTouch开源框架来实现音频变声功能。可以使用SoundTouch对录音文件进行处理,实现变声效果。同,也可以对已有的音频文件进行变声处理。 在使用SoundTouch框架实现变声功能,可以参考源码SoundTouchDemo。该程序使用SoundTouch框架对录音文件进行处理,达到变声的效果。 另外,可以对WAV格式的音频文件进行转码,以实现变声功能。可以将变声后的WAV文件转换成MP3格式,并进行播放。这样可以实现实变声效果,并且减小文件大小。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SoundTouch音频变声案例](https://download.csdn.net/download/weixin_38545923/16395597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [音频变声(soundTouch)V2.0 兼容64位机型](https://download.csdn.net/download/weixin_38699302/16423451)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [录制音频并变声且WAV转MP3](https://download.csdn.net/download/weixin_38617413/16424581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值