分類: android2013-04-13 11:25 606人閱讀 評論(0) 收藏 舉報java
androidAudioframeworksandroid
android調整音量方法有兩種,一種是漸進式,即像手動按音量鍵同樣,一步一步增長或減小,另外一種是直接設置音量值.
下面先分析第一種漸進式的:
app
[java] view plaincopy函數
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 佈局
public void adjustStreamVolume (int streamType, int direction, int flags) post
am.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); spa
解釋一下三個參數
第一個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()方法能夠看到以下代碼:
.net
[java] view plaincopyxml
public void adjustStreamVolume(int streamType, int direction, int flags) { blog
IAudioService service = getService();
try {
service.adjustStreamVolume(streamType, direction, flags);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in adjustStreamVolume", e);
}
}
從代碼裏面能夠看到,這裏是調用的AudioService裏面的adjustStreamVolume()方法,而AudioService的實現文件是:AudioService.java,其方法實現以下:
[java] view plaincopy
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);
}
下面先來看看畫UI的過程:
跟進VolumePanel,發現這個類是一個handle,在postVolumeChanged()方法裏面有以下代碼:
[java] view plaincopy
public void postVolumeChanged(int streamType, int flags) {
if (hasMessages(MSG_VOLUME_CHANGED)) return;
removeMessages(MSG_FREE_RESOURCES);
obtainMessage(MSG_VOLUME_CHANGED, streamType, flags).sendToTarget();
}
這裏利用了android裏面的消息機制來傳遞消息。對android的消息機制有所瞭解的應該知道,這個sendToTarget()方法實際上最後的Target就是它自己,也就是VolumePanel這個類自己,所以咱們去這個Handle的handleMessage()方法裏面查找對於MSG_VOLUME_CHANGED這個類型消息的處理:
[java] view plaincopy
case MSG_VOLUME_CHANGED: {
onVolumeChanged(msg.arg1, msg.arg2);
break;
}
能夠看到,後續是在onVolumeChanged()這個方法裏面處理的,其兩個參數分別是streamType和flags,其中streamType是要調整的音量類型,而flags是傳過來的UI類型。onVolumeChanged()方法代碼以下:
[java] view plaincopy
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);
}
經過代碼能夠知道,根據傳進去的flags不一樣,有不一樣的處理,下面就看看onShowVolumeChanged()方法的處理,也就是ProgressBar的顯示:
[java] view plaincopy
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.getActualDefaultRingtoneUri(
mContext, RingtoneManager.TYPE_RINGTONE);
Uri ringTwoUri = RingtoneManager.getActualDefaultRingtoneUri(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: { //通話聲音的處理
/*
* For in-call voice call volume, there is no inaudible volume.
* Rescale the UI control so the progress bar doesn't go all
* the way to zero and don't show the mute icon.
*/
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.getActualDefaultRingtoneUri(
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?不知道是什麼東西。。
/*
* For in-call voice call volume, there is no inaudible volume.
* Rescale the UI control so the progress bar doesn't go all
* the way to zero and don't show the mute icon.
*/
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.isStreamAffectedByRingerMode(streamType) &&
mAudioService.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE &&
mAudioService.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER)) {
sendMessageDelayed(obtainMessage(MSG_VIBRATE), VIBRATE_DELAY);
}
}
在通話聲音的處理中,有個setSmallIcon()函數,能夠看到,這個是根據不一樣狀況選擇ProgressBar上面顯示的圖片的。
[java] view plaincopy
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);
}
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():
[java] view plaincopy
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);
}
能夠看到,這裏發送了一個廣播,而廣播的內容是:VOLUME_CHANGED_ACTION,也即"android.media.VOLUME_CHANGED_ACTION";當對音量改變事件有興趣時,就能夠接收這個廣播,並作出相應的處理。至此,聲音調整的相關流程就分析的差很少了。