zoukankan      html  css  js  c++  java
  • 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:YES];

        if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {

            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];

        }

        [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];




    #pragma mark - 处理近距离监听触发事件

    -(void)sensorStateChange:(NSNotificationCenter *)notification;

    {

        

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if (isCloseToUser)
        {
            [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
        } else {
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
            if (!_isPlayingAudio) {
                [[EMCDDeviceManager sharedInstance] disableProximitySensor];
            }
        }
        [audioSession setActive:YES error:nil];

    }




    注意事项(也就是我说的问题)
        对于不希望启动接近传感器功能的应用,假设需要进行扬声器和听筒进行切换过程中,则必须通过启用接近传感器来进行声音输出模式的切换。在此时。必需要注意,假设当声音通过听筒进行播放完成时。在播放完成时。此时仍在听筒模式输出。假设此时关闭传感器功能。则导致在离开听筒时,因为传感器功能已经关闭,应用无法再次收到注冊的传感器变更通知,而此时假设未能将底层的声音输出模式切换,则导致相关的声音输出仍从听筒中输出。即使引起传感器反映的障碍已经离开传感器作用范围。但应用中获取的传感器状态仍未接近状态,使依据传感器状态进行切换声音输出模式操作失效。

     

        特殊情况:
    在iPhone 4s及iPhone5中,在接近传感器功能关闭后,假设此时传感器状态为YES,则在再次启动声音传感器时,不会收到传感器的变更通知。
    在iPhone 4中,在接近传感器功能关闭后,假设此时传感器状态为YES,则在再次启动声音传感器时,会先收到一次传感器的变更通知;
       此问题的解决方式:当在传感器功能開始时,假设此时传感器传感状态为YES时。此时声音播放结束。仍未出发传感器状态变更时,此时不关闭传感器功能。当引起传感器反映的障碍已经离开传感器作用范围,此时会收到传感器变更通知,在变更通知中检測当前传感器状态是否为开启状态及声音播放状态。假设在传感器状态为YES时,而此时须要开启传感器功能的操作(如声音播放功能)已经结束时,则将传感器功能关闭就可以;

    -------也就是说。在不是黑屏的状态下。关闭近传感器功能。

    就没什么问题了。


    手动切换两种模式
    解决方式:加入长按手势,切换为还有一种模式。
    代码片段:

    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizeralloc]initWithTarget:self

    action:@selector(longPressed:)];

        [longPressGestureRecognizersetMinimumPressDuration:1.0f];

        [longPressGestureRecognizersetAllowableMovement:50.0];

        [self.bubbleBgImageViewaddGestureRecognizer:longPressGestureRecognizer];

        [longPressGestureRecognizerrelease];


    ---------

    -(void)longPressed:(UILongPressGestureRecognizer *) gestureRecognizer

    {

       switch (gestureRecognizer.state)

        {

            caseUIGestureRecognizerStateEnded:

                

               break;

            caseUIGestureRecognizerStateCancelled:

                

               break;

            caseUIGestureRecognizerStateFailed:

                

               break;

            caseUIGestureRecognizerStateBegan:

               if ([self.voiceDelegaterespondsToSelector:@selector(BaseChartVoiceLongPressed)])

                {

                    [self.voiceDelegateBaseChartVoiceLongPressed];

                }


               break;

            caseUIGestureRecognizerStateChanged:

                

               break;

           default:

               break;

        }

        }


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

    #pragma mark BaseChartCellDelegate

    -(void)BaseChartVoiceLongPressed

    {

        NSLog(@"voice long Pressed");

        

        if ([[[AVAudioSessionsharedInstance]category]isEqualToString:AVAudioSessionCategoryPlayback])

        {

            //切换为听筒播放

            [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

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

            

        }

       else

        {

            //切换为扬声器播放

            [[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlaybackerror:nil];

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

        }

    }



    注意 : 特此声明加入一个声音,播放的第三方下载地址:http://download.csdn.net/detail/wenhaiwang/8998967


  • 相关阅读:
    微信公众号平台接口开发:基础支持,获取微信服务器IP地址
    微信公众号平台接口开发:基础支持,获取access_token
    微信公众号平台接口开发:发送客服消息
    asp.net权限认证篇外:集成域账号登录
    asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)
    asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)
    asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
    asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)
    window配置ftp服务,代码客户端上传下载文件
    制作OpenStack使用的windows镜像
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6913394.html
Copyright © 2011-2022 走看看