在搞rockchip平台时发现,这两种音量在做静音处理时,行为有所不同。
StreamVolume在静音后,调节音量大小会从0开始,不会从静音之前的音量开始调节。
而MasterVolume则能够从静音之前的音量开始。
如何设置默认使用MasterVolume,还是StreamVolume呢!
在AudioManager对象中有对静音按键的处理:
public void handleKeyDown(KeyEvent event, int stream) {
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
/*
* Adjust the volume in on key down since it is more
* responsive to the user.
*/
int flags = FLAG_SHOW_UI | FLAG_VIBRATE;
if (mUseMasterVolume) {
adjustMasterVolume(
keyCode == KeyEvent.KEYCODE_VOLUME_UP
? ADJUST_RAISE
: ADJUST_LOWER,
flags);
} else {
adjustSuggestedStreamVolume(
keyCode == KeyEvent.KEYCODE_VOLUME_UP
? ADJUST_RAISE
: ADJUST_LOWER,
stream,
flags);
mVolume = getStreamVolume(STREAM_MUSIC);
Log.d(TAG,"mVolume = "+mVolume);
}
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
if (event.getRepeatCount() == 0) {
if (mUseMasterVolume) {
setMasterMute(!isMasterMute());
} else {
// TODO: Actually handle MUTE.
if(getStreamVolume(STREAM_MUSIC) != 0)
{
mVolume = getStreamVolume(STREAM_MUSIC);
Log.d(TAG,"mVolume = "+mVolume);
setStreamVolume(STREAM_MUSIC, 0, FLAG_SHOW_UI);
}
else
{
Log.d(TAG,"mVolume = "+mVolume);
setStreamVolume(STREAM_MUSIC, mVolume, FLAG_SHOW_UI);
}
}
}
break;
}
}
mUseMasterVolume控制了使用streamvolume还是mastervolume。
而mUseMasterVolume是从AudioManager的构造函数中初始化的
mUseMasterVolume = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useMasterVolume);
com.android.internal.R.bool.config_useMasterVolume又是什么呢?是framework的配置信息。
可以从frameworks/base/core/res/res下的values/config.xml中找到如下一行:
<bool name="config_useMasterVolume">false</bool>
把false改为true即可。