Android手机上的需要音量调节有六大类铃音,分别是通话音、系统音、铃音、媒体音、闹钟音和通知音,不过音量键只有加大与减少两个键,当用户按音量增加键时,应用怎么知道用户希望加大那类铃音的音量呢?
要解决这个问题,最好是弹出一个对话框,让用户选择希望调节的铃音类型,并显示拖动条,方便用户把音量一次调整到位,不必连续按增加键或减少键。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<SeekBar
android:id="@+id/sb_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:max="100"
android:thumb="@drawable/seekbar_point" />
LinearLayout>
LinearLayout>
package com.fukaimei.keyevent.widget;
import android.app.Dialog;
import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.fukaimei.keyevent.R;
public class VolumeDialog implements OnSeekBarChangeListener, OnKeyListener {
private Dialog dialog;
private View view;
private SeekBar sb_music;
private AudioManager mAudioMgr;
private int MUSIC = AudioManager.STREAM_MUSIC;
private int mMaxVolume;
private int mNowVolume;
public VolumeDialog(Context context) {
mAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mMaxVolume = mAudioMgr.getStreamMaxVolume(MUSIC);
mNowVolume = mAudioMgr.getStreamVolume(MUSIC);
view = LayoutInflater.from(context).inflate(R.layout.dialog_volume, null);
dialog = new Dialog(context, R.style.VolumeDialog);
sb_music = (SeekBar) view.findViewById(R.id.sb_music);
sb_music.setOnSeekBarChangeListener(this);
sb_music.setProgress(sb_music.getMax() * mNowVolume / mMaxVolume);
}
public void show() {
dialog.getWindow().setContentView(view);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
dialog.show();
sb_music.setFocusable(true);
sb_music.setFocusableInTouchMode(true);
sb_music.setOnKeyListener(this);
}
public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
public boolean isShowing() {
if (dialog != null) {
return dialog.isShowing();
} else {
return false;
}
}
public void adjustVolume(int direction, boolean fromActivity) {
if (direction == AudioManager.ADJUST_RAISE) {
mNowVolume += 1;
} else {
mNowVolume -= 1;
}
sb_music.setProgress(sb_music.getMax() * mNowVolume / mMaxVolume);
mAudioMgr.adjustStreamVolume(MUSIC, direction, AudioManager.FLAG_PLAY_SOUND);
if (mListener != null && fromActivity != true) {
mListener.onVolumeAdjust(mNowVolume);
}
close();
}
private void close() {
mHandler.removeCallbacks(mClose);
mHandler.postDelayed(mClose, 2000);
}
private Handler mHandler = new Handler();
private Runnable mClose = new Runnable() {
@Override
public void run() {
dismiss();
}
};
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mNowVolume = mMaxVolume * seekBar.getProgress() / seekBar.getMax();
mAudioMgr.setStreamVolume(MUSIC, mNowVolume, AudioManager.FLAG_PLAY_SOUND);
if (mListener != null) {
mListener.onVolumeAdjust(mNowVolume);
}
close();
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_DOWN) {
adjustVolume(AudioManager.ADJUST_RAISE, false);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
adjustVolume(AudioManager.ADJUST_LOWER, false);
return true;
} else {
return false;
}
}
private VolumeAdjustListener mListener;
public void setVolumeAdjustListener(VolumeAdjustListener listener) {
mListener = listener;
}
public static interface VolumeAdjustListener {
public abstract void onVolumeAdjust(int volume);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_volume"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按下加减键调整音乐音量"
android:textColor="@color/black"
android:textSize="17sp" />
LinearLayout>
package com.fukaimei.keyevent;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.TextView;
import com.fukaimei.keyevent.widget.VolumeDialog;
public class VolumeSetActivity extends AppCompatActivity implements VolumeDialog.VolumeAdjustListener {
private TextView tv_volume;
private VolumeDialog dialog;
private AudioManager mAudioMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volume_set);
tv_volume = (TextView) findViewById(R.id.tv_volume);
mAudioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_DOWN) {
showVolumeDialog(AudioManager.ADJUST_RAISE);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
showVolumeDialog(AudioManager.ADJUST_LOWER);
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return false;
} else {
return false;
}
}
private void showVolumeDialog(int direction) {
if (dialog == null || dialog.isShowing() != true) {
dialog = new VolumeDialog(this);
dialog.setVolumeAdjustListener(this);
dialog.show();
}
dialog.adjustVolume(direction, true);
onVolumeAdjust(mAudioMgr.getStreamVolume(AudioManager.STREAM_MUSIC));
}
@Override
public void onVolumeAdjust(int volume) {
tv_volume.setText("调节后的音乐音量大小为:" + volume);
}
}