先来说方法:调用 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(