获取当前物理输出设备的类型

来源:互联网 发布:淘宝卖家怎么开通微淘 编辑:程序博客网 时间:2024/05/01 20:57

先来说方法:调用 AudioSystem 的 System getDeviceConnectionState(Device, device_add),用不同的 Deveice 依次调用这个函数,如果返回值为 DEVICE_STATE_AVAILABLE,则说明当前判断的 Deveice 就是当前的输出设备类型。

         其中第二个参数 deveice_add 不能赋值为空指针, Android 内部没有判断它是否为空,直接就拿过来 * 了。
         Device 的输入顺序按照 uint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache) 函数中获取 Deveice 的顺序来排列。
         STRATEGY_MEDIA 模式下的判断 Device 顺序为 DEVICE_OUT_WIRED_HEADPHONE, DEVICE_OUT_WIRED_HEADSET,DEVICE_OUT_SPEAKER。
 reason:(1)audio_io_handle_t AudioPolicyManagerBase::getOutput 函数会根据模式调用 uint32_t device = getDeviceForStrategy(strategy) 来得到 Deveice。
            (2)getDeviceForStrategy 的获取 Device 的方法就是依次按照顺序与成员变量 mAvailableOutputDevices 位与,一旦位与结果为 1,就将设备类型设置为该类型。
            (3)mAvailableOutputDevices 只在初始化和 void AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs) 中赋值。
            (4)现在来看 setOutputDevice,这个函数将 Device 位或到 mAvailableOutputDevices 中,如果是 A2DP 蓝牙的话,还会去判断是否可以连接成功,如果失败,还会再将 Device 用反位位与掉。
                 设置好 OutPut 后,会再从 mAvailableOutputDevices 中获取最新的 Device,然后按最新的 Device setOutputDevice(mHardwareOutput, newDevice);
            (5)播放设备的句柄为 mHardwareOutput,在初始化的时候,将它初始化为用 DEVICE_OUT_SPEAKER 打开的设备。

 

调用 getDeviceConnectionState 时需要权限 android.permission.MODIFY_AUDIO_SETTINGS.

在 Manifest.xml 里 <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />s