码迷,mamicode.com
首页 > 移动开发 > 详细

手机影音7--视频播放器的基本功能(4)

时间:2016-11-15 08:00:48      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:over   set   return   override   视频播放   ptr   变声   tracking   seekbar   

技术分享

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;

@Override
public 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.监听按手机声音健改变声音大小  

@Override
public 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);
}
 

  

  

  

手机影音7--视频播放器的基本功能(4)

标签:over   set   return   override   视频播放   ptr   变声   tracking   seekbar   

原文地址:http://www.cnblogs.com/ganchuanpu/p/6064191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!