转自:http://blog.csdn.net/bmj/article/details/8796421
android调整音量方法有两种,一种是渐进式,即像手动按音量键一样,一步一步增加或减少,另一种是直接设置音量值.
下面先分析第一种渐进式的:
解释一下三个参数
第一个streamType是需要调整音量的类型,这里设的是媒体音量,可以是:
STREAM_ALARM 警报
STREAM_MUSIC 音乐回放即媒体音量
STREAM_NOTIFICATION 窗口顶部状态栏Notification,
STREAM_RING 铃声
STREAM_SYSTEM 系统
STREAM_VOICE_CALL 通话
STREAM_DTMF 双音多频,不是很明白什么东西
第二个direction,是调整的方向,增加或减少,可以是:
ADJUST_LOWER 降低音量
ADJUST_RAISE 升高音量
ADJUST_SAME 保持不变,这个主要用于向用户展示当前的音量
第三个flags是一些附加参数,只介绍两个常用的
FLAG_PLAY_SOUND 调整音量时播放声音
FLAG_SHOW_UI 调整时显示音量条,就是按音量键出现的那个
0 表示什么也没有
首先跟进AudioManager的adjustStreamVolume()方法可以看到如下代码:
从代码里面可以看到,这里是调用的AudioService里面的adjustStreamVolume()方法,而AudioService的实现文件是:AudioService.java,其方法实现如下:
下面先来看看画UI的过程:
跟进VolumePanel,发现这个类是一个handle,在postVolumeChanged()方法里面有如下代码:
这里利用了android里面的消息机制来传递消息。对android的消息机制有所了解的应该知道,这个sendToTarget()方法实际上最后的Target就是它本身,也就是VolumePanel这个类本身,因此我们去这个Handle的handleMessage()方法里面查找对于MSG_VOLUME_CHANGED这个类型消息的处理:
可以看到,后续是在onVolumeChanged()这个方法里面处理的,其两个参数分别是streamType和flags,其中streamType是要调整的音量类型,而flags是传过来的UI类型。onVolumeChanged()方法代码如下:
通过代码可以知道,根据传进去的flags不同,有不同的处理,下面就看看onShowVolumeChanged()方法的处理,也就是ProgressBar的显示:
在通话声音的处理中,有个setSmallIcon()函数,可以看到,这个是根据不同情况选择ProgressBar上面显示的图片的。
View view = mView =inflater.inflate(com.android.internal.R.layout.volume_adjust,null);
mLevel就是显示的那个ProgressBar,mLevel = (ProgressBar)view.findViewById(com.android.internal.R.id.level);
从这里我们可以看到,声音调整显示的布局文件是volume_adjust.xml,如果想自己对声音显示的布局进行调整的话,就可以自己手动修改这个布局文件,达到自己想要的效果了。
到这里就把声音调整的UI显示过程分析完了,下面接着来分析声音调整广播发送sendVolumeUpdate():
可以看到,这里发送了一个广播,而广播的内容是:VOLUME_CHANGED_ACTION,也即"android.media.VOLUME_CHANGED_ACTION";当对音量改变事件有兴趣时,就可以接收这个广播,并做出相应的处理。至此,声音调整的相关流程就分析的差不多了。
- AudioManager
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); - public
void adjustStreamVolume (int streamType, int direction, int flags) - am.adjustStreamVolume
(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
第一个streamType是需要调整音量的类型,这里设的是媒体音量,可以是:
STREAM_ALARM 警报
STREAM_MUSIC 音乐回放即媒体音量
STREAM_NOTIFICATION 窗口顶部状态栏Notification,
STREAM_RING 铃声
STREAM_SYSTEM 系统
STREAM_VOICE_CALL 通话
STREAM_DTMF 双音多频,不是很明白什么东西
ADJUST_LOWER 降低音量
ADJUST_RAISE 升高音量
ADJUST_SAME 保持不变,这个主要用于向用户展示当前的音量
FLAG_PLAY_SOUND 调整音量时播放声音
FLAG_SHOW_UI 调整时显示音量条,就是按音量键出现的那个
0 表示什么也没有
- public
void adjustStreamVolume(int streamType, int direction, int flags) { -
IAudioService service = getService(); -
try { -
service.adjustStreamVolume(streamType, direction, flags); -
} catch (RemoteException e) { -
Log.e(TAG, "Dead object in adjustStreamVolume", e); -
} -
}
- public
void adjustStreamVolume(int streamType, int direction, int flags) { -
ensureValidDirection(direction); //数据正确性检查 -
ensureValidStreamType(streamType); //数据正确性检查 -
。 -
。 -
。 -
// If stream is muted, adjust last audible index only -
int index; //局部变量,保存调整后的音量状态 -
//进行实际的音量调整,在mAudioHandler里面进行。 -
if (streamState.muteCount() != 0) { -
if (adjustVolume) { -
streamState.adjustLastAudibleIndex(direction); -
// Post a persist volume msg -
sendMsg(mAudioHandler, MSG_PERSIST_VOLUME, streamType, -
SENDMSG_REPLACE, 0, 1, streamState, PERSIST_DELAY); -
} -
index = streamState.mLastAudibleIndex; -
} else { -
if (adjustVolume && streamState.adjustIndex(direction)) { -
// Post message to set system volume (it in turn will post a message -
// to persist). Do not change volume if stream is muted. -
sendMsg(mAudioHandler, MSG_SET_SYSTEM_VOLUME, STREAM_VOLUME_ALIAS[streamType], SENDMSG_NOOP, 0, 0, -
streamState, 0); -
} -
index = streamState.mIndex; -
} -
-
// UI //画UI,即调整音量时出现的那个ProgressBar -
mVolumePanel.postVolumeChanged(streamType, flags); -
// Broadcast Intent //发送广播,广播音量有改变的系统事件 -
sendVolumeUpdate(streamType, oldIndex, index); -
}
- public
void postVolumeChanged(int streamType, int flags) { -
if (hasMessages(MSG_VOLUME_CHANGED)) return; -
removeMessages(MSG_FREE_RESOURCES); -
obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget(); -
}
- case
MSG_VOLUME_CHANGED: { -
onVolumeChanged(msg.arg1, msg.arg2); -
break; -
}
- protected
void onVolumeChanged(int streamType, int flags) { -
-
-
if (LOGD) Log.d(TAG, "onVolumeChanged(streamType: " + streamType + ", flags: " + flags + ")"); -
-
//根据flags的不同,来做不同的处理 -
if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { -
onShowVolumeChanged(streamType, flags);//UI显示 -
} -
-
-
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); -
}
- protected
void onShowVolumeChanged(int streamType, int flags) { -
int index = mAudioService.getStreamVolume(streamType); -
int message = UNKNOWN_VOLUME_TEXT; -
int additionalMessage = 0; -
mRingIsSilent = false; -
-
-
if (LOGD) { -
Log.d(TAG, "onShowVolumeChanged(streamType: " + streamType -
+ ", flags: " + flags + "), index: " + index); -
} -
-
-
// get max volume for progress bar -
int max = mAudioService.getStreamMaxVolume(streamType); -
-
-
switch (streamType) { -
-
-
case AudioManager.STREAM_RING: { //铃声的处理 -
setRingerIcon(); -
message = RINGTONE_VOLUME_TEXT; -
Uri ringuri = RingtoneManager.getActualDefaultRingtone Uri( -
mContext, RingtoneManager.TYPE_RINGTONE); -
Uri ringTwoUri = RingtoneManager.getActualDefaultRingtone Uri(mContext, RingtoneManager.TYPE_RINGTONE, -
PhoneFactory.RAW_PHONE_ID); -
if ((ringuri == null) && (ringTwoUri == null)) { -
additionalMessage = -
//com.android.internal.R.string.volume_music_hint_silent_ringtone_selected; -
com.android.internal.R.string.volume_music_hint_sim1_and_sim2_silent_ringtone_selected; -
mRingIsSilent = true; -
} else if ((ringuri == null) && (ringTwoUri != null)) { -
additionalMessage = -
com.android.internal.R.string.volume_music_hint_silent_sim1_ringtone_selected; -
} else if ((ringuri != null) && (ringTwoUri == null)) { -
additionalMessage = -
com.android.internal.R.string.volume_music_hint_sim2_silent_ringtone_selected; -
} -
break; -
} -
-
-
case AudioManager.STREAM_MUSIC: { //音乐声音的处理 -
message = MUSIC_VOLUME_TEXT; -
if (mAudioManager.isBluetoothA2dpOn()) { -
additionalMessage = -
com.android.internal.R.string.volume_music_hint_playing_through_bluetooth; -
setLargeIcon(com.android.internal.R.drawable.ic_volume_bluetooth_ad2p); -
} else { -
setSmallIcon(index); -
} -
break; -
} -
-
-
case AudioManager.STREAM_FM: { //FM声音的处理 -
message = FM_VOLUME_TEXT; -
setSmallIcon(index); -
break; -
} -
-
-
case AudioManager.STREAM_VOICE_CALL: { //通话声音的处理 -
-
index++; -
max++; -
message = INCALL_VOLUME_TEXT; -
setSmallIcon(index); -
break; -
} -
-
-
case AudioManager.STREAM_ALARM: { //闹钟声音的处理 -
message = ALARM_VOLUME_TEXT; -
setSmallIcon(index); -
break; -
} -
-
-
case AudioManager.STREAM_NOTIFICATION: { //Notification声音的处理 -
message = NOTIFICATION_VOLUME_TEXT; -
setSmallIcon(index); -
Uri ringuri = RingtoneManager.getActualDefaultRingtone Uri( -
mContext, RingtoneManager.TYPE_NOTIFICATION); -
if (ringuri == null) { -
additionalMessage = -
com.android.internal.R.string.volume_music_hint_silent_ringtone_selected; -
mRingIsSilent = true; -
} -
break; -
} -
-
-
case AudioManager.STREAM_BLUETOOTH_SCO: { //蓝牙_sco?不知道是什么东西。。 -
-
index++; -
max++; -
message = BLUETOOTH_INCALL_VOLUME_TEXT; -
setLargeIcon(com.android.internal.R.drawable.ic_volume_bluetooth_in_call); -
break; -
} -
} -
-
-
String messageString = Resources.getSystem().getString(message); //根据调整的声音不同,显示不同的信息 -
if (!mMessage.getText().equals(messageString)) { -
mMessage.setText(messageString); -
} -
-
-
if (additionalMessage == 0) { -
mAdditionalMessage.setVisibility(View.GONE); -
} else { -
mAdditionalMessage.setVisibility(View.VISIBLE); -
mAdditionalMessage.setText(Resources.getSystem().getString(additionalMessage)); -
} -
-
-
if (max != mLevel.getMax()) { -
mLevel.setMax(max); -
} -
mLevel.setProgress(index); //设置ProgressBar的值 -
-
-
mToast.setView(mView); -
mToast.setDuration(Toast.LENGTH_SHORT); -
mToast.setGravity(Gravity.TOP, 0, 0); -
mToast.show(); -
-
-
// Do a little vibrate if applicable (only when going into vibrate mode) -
if ((flags & AudioManager.FLAG_VIBRATE) != 0 && -
mAudioService.isStreamAffectedByRinger Mode(streamType) && -
mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE && -
mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) { -
sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY); -
} -
}
- private
void setSmallIcon(int index) { -
mLargeStreamIcon.setVisibility(View.GONE); -
mSmallStreamIcon.setVisibility(View.VISIBLE); -
-
-
mSmallStreamIcon.setImageResource(index == 0 -
? com.android.internal.R.drawable.ic_volume_off_small -
: com.android.internal.R.drawable.ic_volume_small); -
}
- private
void sendVolumeUpdate(int streamType, int oldIndex, int index) { -
oldIndex = (oldIndex + 5) / 10; -
index = (index + 5) / 10; -
-
-
Intent intent = new Intent(AudioManager.VOLUME_CHANGED_ACTION); -
intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, streamType); -
intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, index); -
intent.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); -
-
-
mContext.sendBroadcast(intent); -
}