Platform: Rockchip
OS: Android 6.0
Kernel: 3.10.92
由于Audio原始设计DTMF stream最终转换成MUSIC stream, 现在想把DTMF独立出来单独使用,因此修改
STREAM_VOLUME_ALIAS_DEFAULT数组
private final int[] STREAM_VOLUME_ALIAS_DEFAULT = new int[] {
AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL
AudioSystem.STREAM_RING, // STREAM_SYSTEM
AudioSystem.STREAM_RING, // STREAM_RING
AudioSystem.STREAM_MUSIC, // STREAM_MUSIC
AudioSystem.STREAM_ALARM, // STREAM_ALARM
AudioSystem.STREAM_RING, // STREAM_NOTIFICATION
AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO
AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED
//把AudioSystem.STREAM_MUSIC替换成AudioSystem.STREAM_DTMF
AudioSystem.STREAM_DTMF, // STREAM_DTMF
AudioSystem.STREAM_MUSIC // STREAM_TTS
};
private void updateStreamVolumeAlias(boolean updateVolumes, String caller) {
int dtmfStreamAlias;
switch (mPlatformType) {
case AudioSystem.PLATFORM_VOICE:
mStreamVolumeAlias = STREAM_VOLUME_ALIAS_VOICE;
dtmfStreamAlias = AudioSystem.STREAM_RING;
break;
case AudioSystem.PLATFORM_TELEVISION:
mStreamVolumeAlias = STREAM_VOLUME_ALIAS_TELEVISION;
dtmfStreamAlias = AudioSystem.STREAM_MUSIC;
break;
default:
mStreamVolumeAlias = STREAM_VOLUME_ALIAS_DEFAULT;
dtmfStreamAlias = AudioSystem.STREAM_MUSIC;
}
//不管是什么平台,都设置成AudioSystem.STREAM_DTMF
dtmfStreamAlias = AudioSystem.STREAM_DTMF;
......
}
修改开机后发生了exception:
01-03 15:34:39.982 553 553 W SystemServer: ***********************************************
01-03 15:34:39.983 553 553 E SystemServer: BOOT FAILURE starting Audio Service
01-03 15:34:39.983 553 553 E SystemServer: java.lang.ArrayIndexOutOfBoundsException: length=7; index=8
01-03 15:34:39.983 553 553 E SystemServer: at com.android.server.audio.AudioService.createStreamStates(AudioService.java:903)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.server.audio.AudioService.<init>(AudioService.java:639)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.server.SystemServer.startOtherServices(SystemServer.java:874)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.server.SystemServer.run(SystemServer.java:279)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.server.SystemServer.main(SystemServer.java:171)
01-03 15:34:39.983 553 553 E SystemServer: at java.lang.reflect.Method.invoke(Native Method)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:772)
01-03 15:34:39.983 553 553 E SystemServer: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
是createStreamStates()引起:
private void createStreamStates() {
int numStreamTypes = AudioSystem.getNumStreamTypes();
VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes];
for (int i = 0; i < numStreamTypes; i++) {
//这行数组越界
streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i);
}
}
VOLUME_SETTINGS:
/**
* The mapping of stream type (integer) to its setting.
*
* @removed Not used by anything since API 2.
*/
public static final String[] VOLUME_SETTINGS = {
VOLUME_VOICE, VOLUME_SYSTEM, VOLUME_RING, VOLUME_MUSIC,
VOLUME_ALARM, VOLUME_NOTIFICATION, VOLUME_BLUETOOTH_SCO
};
而AudioSystem.STREAM_DTMF对应的值是8, 越界了。
看注释,此数组已经被弃用了,估计不维护了,但是在createStreamStates()却引用了这个字符串数组,导致异常产生。
解决方法:
private void createStreamStates() {
int numStreamTypes = AudioSystem.getNumStreamTypes();
VolumeStreamState[] streams = mStreamStates = new VolumeStreamState[numStreamTypes];
for (int i = 0; i < numStreamTypes; i++) {
//判断如果是DTMF,就另外赋值
if (i == 8)
streams[i] = new VolumeStreamState("volume_dtmf", i);
else
streams[i] = new VolumeStreamState(System.VOLUME_SETTINGS[mStreamVolumeAlias[i]], i);
}
checkAllFixedVolumeDevices();
checkAllAliasStreamVolumes();
checkMuteAffectedStreams();
}
}