你的位置:首页 > 操作系统

[操作系统]手机影音7


1.用AudioManager调声音

1_得到当前音量和最大音量

//关于设置音量private int currentVolume;//当前音量值private int currentMaxVolume;//当前最大音量private AudioManager am;am = (AudioManager) getSystemService(AUDIO_SERVICE);   currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);currentMaxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

2_拖动的时候改变音量

voice_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       @Override    public void onStopTrackingTouch(SeekBar seekBar) {     sendDelayedHideControlPlayerMessage();    }       @Override    public void onStartTrackingTouch(SeekBar seekBar) {     removeDelayedHideControlPlayerMessage();    }       @Override    public void onProgressChanged(SeekBar seekBar, int progress,      boolean fromUser) {     if(fromUser){      updateVolume(progress);     }    }});/** * 修改音量范围0~15 * @param progress2*/protected void updateVolume(int volume) {  am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);  voice_seekBar.setProgress(volume);    currentVolume = volume;}

3_静音和非静音

case R.id.btn_voice:	isMute = !isMute;	updateVolume(currentVolume);break;/** * 修改音量范围0~15 * @param progress2*/protected void updateVolume(int volume) {	if(isMute){		am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);		voice_seekBar.setProgress(0);	}else{		am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);		voice_seekBar.setProgress(volume);	} 	currentVolume = volume;}

2.手指在屏幕上下滑动实现声音大小变化  

private float startY;/** * 滑动的区域 */private float touchRang;/** * 当前的音量 */private int mVol;@Overridepublic boolean onTouchEvent(MotionEvent event) {	switch (event.getAction()){		case MotionEvent.ACTION_DOWN://手指按下屏幕			//1.记录相关的值			startY = event.getY();			touchRang =Math.min(screenWidth, screenHeight);//screenHeight			mVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);			handler.removeMessages(HIDE_MEDIACONTROL);			break;		case MotionEvent.ACTION_MOVE://手指在屏幕上移动			//2.来到结束的坐标			float endY = event.getY();			//3.计算偏移量			float distanceY = startY - endY;			//要改变的声音 = (滑动的距离 / 总距离)*最大音量			float delta = (distanceY/touchRang)*maxVolume;			//最终声音 = 原来的声音 + 要改变的声音			float volum e = Math.min(Math.max(mVol+delta,0),maxVolume);			if(delta != 0){				updatavolumeProgress((int) volume);			}			// startY = event.getY();			break;		case MotionEvent.ACTION_UP://手指离开屏幕			handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROL,5000);			break;	}	return super.onTouchEvent(event);}

3.监听按手机声音健改变声音大小  

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {  if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){    currentVolume--;    updatavolumeProgress(currentVolume);    handler.removeMessages(HIDE_MEDIACONTROL);    handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROL, 5000);    return true;  }else if(keyCode ==KeyEvent.KEYCODE_VOLUME_UP){    currentVolume++;    updatavolumeProgress(currentVolume);    handler.removeMessages(HIDE_MEDIACONTROL);    handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROL, 5000);    return true;  }  return super.onKeyDown(keyCode, event);}