我在录制过程中通过调用AudioRecord.getRoutedDevice();
尝试获取输入音频的当前路由,但在大多数Android API23 +设备上,它返回null
(在300k各种Android设备中,只有10个%返回有意义的信息)。即使在相同的设备型号(三星Galaxy S5,6.0.1)上,70%的设备返回null
,30%的设备返回正确的信息。
我尝试使用反射对其进行调试,并复制了一些SDK代码以逐步完成 - 我可以看到AudioRecord.getRoutedDevice();
内部的native_getRoutedDeviceId()
和{{} { 1}},但是ids不匹配:
getAudioManager().getDevices()
我可能做错了什么,是否还有其他我可以使用的API?
例如@TargetApi(23)
private AudioDeviceInfo getRoutedDevice() {
Object r = (Object)(0);
try {
Method method = _record.getClass().getDeclaredMethod("native_getRoutedDeviceId");
method.setAccessible(true);
r = method.invoke(_record);
} catch (Exception e) {
e.printStackTrace();
}
int deviceId = (int)r; //native_getRoutedDeviceId();
if (deviceId == 0)
return null;
AudioDeviceInfo[] devices =
getAudioManager().getDevices(AudioManager.GET_DEVICES_INPUTS);
for (int i = 0; i < devices.length; i++)
if (devices[i].getId() == deviceId)
return devices[i];
return null;
}
之类的旧API并不能保证音频IO目前正在通过耳机。