接上一篇文章:
第二步:设计自己的volume.java
#####################################################
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volume);
Intent intent = getIntent();//传参数过来。也就是stream的值,默认为STREAM_MUSIC
vol_stream = intent.getIntExtra("vol_stream",3);//STREAM_MUSIC
mSeekBar = (SeekBar) this.findViewById(R.id.volume_progress);
mSeekBar.setOnKeyListener(this);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeIndex = mAudioManager.getStreamVolume(vol_stream);
mMaxVolume=mAudioManager.getStreamMaxVolume(vol_stream);
mSeekBar.setProgress((int) ((float) volumeIndex / mMaxVolume * MAX_PROGRESS));
mContext =getApplicationContext();
mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 80);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.volume_progress:
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
//volumechanged();
volmueincrease();
return true;
}
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
//volumechanged();
volmuedecrease();
return true;
}
}
break;
}
return false;
}
boolean isMusicActive() {//判断是否是在播放中
final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
if (am == null) {
Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
return false;
}
return am.isMusicActive();
}
public void volmue_play()
{
/*这个主要是去播放一段铃声,
defaultUri =RingtoneManager.getActualDefaultRingtoneUri(mContext,AudioProfileManager.TYPE_NOTIFICATION); //RingtoneManager.getValidRingtoneUri(mContext);//Settings.System.DEFAULT_ALARM_ALERT_URI; //
mRingtone = RingtoneManager.getRingtone(mContext, defaultUri);
if (mRingtone != null) {
mRingtone.setStreamType(AudioManager.STREAM_MUSIC);
mRingtone.play();
Log.w(TAG, "mRingtone.play");
}
*/
synchronized(this) {
if(mToneGenerator!=null)
{
Log.w(TAG, "mRingtone.play");
mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);//这个是播放一个按键音的tone,声音短,方便用户操作
}
}
}
public void volmuedecrease(){//vol --
Log.w(TAG, "volmuedecrease#vol_stream"+vol_stream);
volumeIndex = mAudioManager.getStreamVolume(vol_stream);
mMaxVolume=mAudioManager.getStreamMaxVolume(vol_stream);
if (volumeIndex < 1) {
volumeIndex = 0;
} else {
volumeIndex -= 1;
}
mAudioManager.setStreamVolume(vol_stream,
volumeIndex, AudioManager.FLAG_VIBRATE);
mSeekBar.setProgress((int) ((float) volumeIndex / mMaxVolume * MAX_PROGRESS));
Log.w(TAG, "volmuedecrease#isMusicActive"+isMusicActive());
if(vol_stream ==AudioManager.STREAM_MUSIC&&(!isMusicActive()))
{
volmue_play();
}
}
public void volmueincrease(){
Log.w(TAG, "volmueincrease#vol_stream"+vol_stream);
volumeIndex = mAudioManager.getStreamVolume(vol_stream);
mMaxVolume=mAudioManager.getStreamMaxVolume(vol_stream);
if (volumeIndex >= mMaxVolume)
{
volumeIndex = mMaxVolume;
}
else {
volumeIndex += 1;
}
mAudioManager.setStreamVolume(vol_stream,
volumeIndex, AudioManager.FLAG_VIBRATE);
mSeekBar.setProgress((int) ((float) volumeIndex / mMaxVolume * MAX_PROGRESS));
Log.w(TAG, "volmueincrease#isMusicActive"+isMusicActive());
if(vol_stream ==AudioManager.STREAM_MUSIC&&(!isMusicActive()))
{
volmue_play();
}
}
@Override
protected void onStop() {
Log.w(TAG, "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.w(TAG, "onDestroy#in");
super.onDestroy();
/*
if (mRingtone != null) {
Log.w(TAG, "onDestroy#ring stop");
mRingtone.stop();
}
*/
if(mToneGenerator!=null)
{
Log.w(TAG, "onDestroy#ring stop");
mToneGenerator.stopTone();//退出的时候stop tone
}
}
################################################################
说明如下:volmue_play这个主要是在按键之后随着seekbar的变换,有个声音的输出,可以比较直观的了解到音量的变化的效果