zoukankan      html  css  js  c++  java
  • 扬声器听筒的切换

    下午研究了一下扬声器听筒切换

    如果需要保持插拔耳机之前的状态可以用我总结的代码:如果插耳机之前是扬声器,拔了之后还保存扬声器状态。

    if (判断条件) {

            // 扬声器       

       [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord                      withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

         } else {

            // 听筒

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

        }

    如果不需要:(enable是判断条件)

    [[AVAudioSession sharedInstance] overrideOutputAudioPort:enable ? AVAudioSessionPortOverrideSpeaker:AVAudioSessionPortOverrideNone error:nil];

    原文:http://blog.csdn.net/vieri_ch/article/details/43733375

    本文部分翻译了官方对这两个属性的描述。链接如下,此外加入个人的使用体会

    https://developer.apple.com/library/ios/qa/qa1754/_index.html

    问题的起源,在iOS中,播放声音时,输出到扬声器的需求。两个属性设置的区别

    The difference is that setting the AVAudioSessionPortOverride by calling overrideOutputAudioPort: is more transient than using the category option AVAudioSessionCategoryOptionDefaultToSpeaker.

    通过overrideOutputAudioPort这个方法设置,属性AVAudioSessionPortOverride, 比使用AVAudioSessionCategoryOptionDefaultToSpeaker这个属性更短暂

    e.g

    方法1

    [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];

    方法2 ,

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];


    方法1,设置之后,如果此时插入耳机,在拔掉。播放的声音会从听筒输出,而不是回到扬声器

    方法2. 设置之后,始终输出到扬声器,而不是其他接收器,如果没有耳机。(简要的说,就是如果有个蓝牙音箱,哪怕接上都不会有声音输出到蓝牙音响,插上耳机,则会有声音输出到耳机。)

    这里有个很重要的地方。这两个属性都只被用于。AVAudioSessionCategoryPlayAndRecord


    使用场景的补充

    如果在某个场景下,希望强制从speaker输出声音,最好使用

    AVAudioSessionCategoryPlayback, 而不是 AVAudioSessionCategoryPlayAndRecord ,因为后者默认从听筒输出。无外接设备的情况。

     

    [session setCategory:AVAudioSessionCategoryPlayback error:&error];


    需要使用扬声器和听筒切换场景,用

    AVAudioSessionCategoryPlayAndRecord


    使用听筒之后,PlayandRecord下切换到扬声器

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error]; 

    这篇也有一定参考价值:

    http://blog.csdn.net/xdrt81y/article/details/38926663

  • 相关阅读:
    服务器做系统备份时失败
    PHPMailer中文乱码问题的解决方法
    html字符串分行显示
    Oracle中取某几个数的最大值最小值
    分布式事务之 Seata
    org.apache.dubbo 2.7.7 服务端处理请求及时间轮(失败重试)
    org.apache.dubbo 2.7.7 服务消费源码
    org.apache.dubbo 2.7.7 服务发布注册源码
    org.apache.dubbo 2.7.x 再聚首
    spring-cloud-gateway 服务网关
  • 原文地址:https://www.cnblogs.com/whqios/p/5356488.html
Copyright © 2011-2022 走看看