问题描述
我目前面临使用Android Nougat的Android音频政策的问题。 我有一个具有以下设备的Android设备:
- 已连接USB手机
- 音频插孔输出
- 可能是蓝牙设备
如Android文档(https://source.android.com/devices/audio/implement-policy)中所述,这些设备使用特定的audio_policy_configuration.xml声明。
2个应用程序主要在设备上运行:
- 应该只能通过USB手机播放
- 另一个应该只能通过音频插孔播放
如果添加了其他应用,则该应用也只能通过音频插孔播放(我们设备的默认行为)
但是,当前的行为是,如果连接了USB,则USB会接管所有声音。我对Android音频政策的C ++代码进行了深入研究,但是我很难尽全力。
我知道Android正在提供一个API来强制应用程序的音频输出,但是我想知道是否可以通过配置来修改Android直接处理其输出的方式。理想情况下,仍然定义所有不同的输出,但仅将音频插孔作为“默认输出”。
在较旧的Android版本中,此命令似乎是硬编码的:
// when not in a phone call,phone strategy should route STREAM_VOICE_CALL to A2DP
if (!isInCall() &&
(mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
(outputs.getA2dpOutput() != 0)) {
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
if (device) break;
}
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
if (device) break;
if (!isInCall()) {
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
if (device) break;
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
if (device) break;
}
device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
if (device) break;
device = mApmObserver->getDefaultOutputDevice()->type();
if (device == AUDIO_DEVICE_NONE) {
ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
}
break;
当Nougat +返回设备列表时:
devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
if (!devices.isEmpty()) break;
// when not in a phone call,phone strategy should route STREAM_VOICE_CALL to A2DP
if (!isInCall() &&
(getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
outputs.isA2dpSupported()) {
devices = availableOutputDevices.getFirstDevicesFromTypes({
AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
if (!devices.isEmpty()) break;
}
devices = availableOutputDevices.getFirstDevicesFromTypes({
AUDIO_DEVICE_OUT_WIRED_HEADPHONE,AUDIO_DEVICE_OUT_WIRED_HEADSET,AUDIO_DEVICE_OUT_LINE,AUDIO_DEVICE_OUT_USB_HEADSET,AUDIO_DEVICE_OUT_USB_DEVICE});
if (!devices.isEmpty()) break;
if (!isInCall()) {
devices = availableOutputDevices.getFirstDevicesFromTypes({
AUDIO_DEVICE_OUT_USB_ACCESSORY,AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,AUDIO_DEVICE_OUT_AUX_DIGITAL,AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
if (!devices.isEmpty()) break;
}
devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
break;
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)