最近在查Android5.1 bug:插入耳机,音量按键调节音量:耳机和speaker都有声音;就屡屡耳机提示音这块代码;做个记录;
/**
* Override this if you have other work to do when the volume changes (for
* example, vibrating, playing a sound, etc.). Make sure to call through to
* the superclass implementation.
*/
/*streamType默认是:STREAM_RING;播放歌曲时是:STREAM_MUSIC;默认按下音量调节按键则调节
*的是STREM_RING音量;
*flag:FLAG_SHOW_UI就是按下音量按键弹出下来弹窗;
*flag:FLAG_PLAY_SOUND就是播放铃声;STREAM_RING才播放铃声;mRingIsSilent在
*onShowVolumeChanged根据STREN_RING置为true;
*/
protected void onVolumeChanged(int streamType, int flags) {
if (LOGD) Log.d(mTag, "onVolumeChanged(streamType: " + streamToString(streamType)
+ ", flags: " + flagsToString(flags) + ")");
if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
synchronized (this) {
if (mActiveStreamType != streamType) {
reorderSliders(streamType);
}
//根据streamType显示不同弹窗
onShowVolumeChanged(streamType, flags, null);
}
}
//STREAM_RING发送播放铃声广播;STREAM_MUSIC不发送;
if ((flags & AudioManager.FLAG_PLAY_SOUND) != 0 && ! mRingIsSilent) {
removeMessages(MSG_PLAY_SOUND);
sendMessageDelayed(obtainMessage(MSG_PLAY_SOUND, streamType, flags), PLAY_SOUND_DELAY);
}
if ((flags & AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE) != 0) {
removeMessages(MSG_PLAY_SOUND);
removeMessages(MSG_VIBRATE);
onStopSounds();
}
removeMessages(MSG_FREE_RESOURCES);
sendMessageDelayed(obtainMessage(MSG_FREE_RESOURCES), FREE_DELAY);
resetTimeout();
}
case MSG_PLAY_SOUND: {
onPlaySound(msg.arg1, msg.arg2);
break;
}
protected void onPlaySound(int streamType, int flags) {
if (hasMessages(MSG_STOP_SOUNDS)) {
removeMessages(MSG_STOP_SOUNDS);
// Force stop right now
onStopSounds();
}
synchronized (this) {
ToneGenerator toneGen = getOrCreateToneGenerator(streamType);
if (toneGen != null) {
//STREAM_RING播放音量调节提示音
toneGen.startTone(ToneGenerator.TONE_PROP_BEEP);
sendMessageDelayed(obtainMessage(MSG_STOP_SOUNDS), BEEP_DURATION);
}
}
}