手势调整视频播放进度及音量

目前很多视频播放器都支持手势调整视频播放进度及音量,最近在项目中也实现了这一功能,废话不多说,直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//实现手势调整播放进度和音量
private int mThreshold = 30;//滑动处理的阀值
private float mDownX;//手指按下时的x
private float mDownY;
private boolean mChangePosition = false;
private boolean mChangeVolume = false;
private int mCurrentPosition;
private int mCurrentVolume;
private int mCurrentProgress;
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
int id = v.getId();
if (id == R.id.video_view) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = x;
mDownY = y;
mChangeVolume = false;
mChangePosition = false;
break;
case MotionEvent.ACTION_MOVE:
float deltaX = x - mDownX;
float deltaY = y - mDownY;
float absDeltaX = Math.abs(deltaX);
float absDeltaY = Math.abs(deltaY);
if (!mChangePosition && !mChangeVolume) {
if (absDeltaX > mThreshold || absDeltaY > mThreshold) {
if (absDeltaX >= mThreshold) {
mChangePosition = true;
mCurrentPosition = mMediaPlayer.getCurrentPosition();
} else {
mChangeVolume = true;
mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}
}
}
if (mChangePosition) {
showProgress(deltaX);
}
if (mChangeVolume) {
showVolume(-deltaY);
}
break;
case MotionEvent.ACTION_UP:
if (mChangePosition) {
mMediaPlayer.seekTo(mCurrentProgress);
int duration = mMediaPlayer.getDuration();
int progress = mCurrentProgress * 100 / (duration == 0 ? 1 : duration);
mControllerView.getProgress().setProgress(progress);
ViewAnimator.animate(tv_progress_time).alpha(1.0f, 0f).duration(500).onStop(new AnimationListener.Stop() {
@Override
public void onStop() {
tv_progress_time.setAlpha(1.0f);
tv_progress_time.setVisibility(GONE);
}
}).start();
}
if (mChangeVolume) {
ViewAnimator.animate(fl_volume).alpha(1.0f, 0f).duration(500).onStop(new AnimationListener.Stop() {
@Override
public void onStop() {
fl_volume.setAlpha(1.0f);
fl_volume.setVisibility(GONE);
}
}).start();
}
break;
}
} else if (id == R.id.progress) {
//这里对播放进度控制模块的触摸事件不做响应
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
ViewParent vpdown = getParent();
while (vpdown != null) {
vpdown.requestDisallowInterceptTouchEvent(true);//告诉父控件不要拦截子空间的触摸事件
vpdown = vpdown.getParent();
}
break;
case MotionEvent.ACTION_UP:
ViewParent vpup = getParent();
while (vpup != null) {
vpup.requestDisallowInterceptTouchEvent(false);
vpup = vpup.getParent();
}
break;
}
}
return true;
}
/**
* 显示播放进度
*
* @param deltaX
*/
private void showProgress(float deltaX) {
int screenWidth = ViewUtils.getScreenWidth(getContext());//获取屏幕的宽度
int totalTime = 0;
if ( mMediaPlayer.isPlaying()) {
totalTime = mMediaPlayer.getDuration();
}
mCurrentProgress = (int) (mCurrentPosition + deltaX * totalTime / screenWidth);
mCurrentProgress = mCurrentProgress >= totalTime ? totalTime : mCurrentProgress;
tv_progress_time.setVisibility(VISIBLE);
tv_progress_time.setText(DateFormatUtils.stringForTime(mCurrentProgress) + " / " + DateFormatUtils.stringForTime(totalTime));
}
/**
* 显示音量
*
* @param deltaY
*/
private void showVolume(float deltaY) {
int screenHeight = ViewUtils.getScreenHeight(mContext);//获取屏幕的高度
int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int deltaV = (int) (max * deltaY / screenHeight);//这里如果觉得滑动改变的值太小,可以将deltaY乘以某个系数即可
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mCurrentVolume + deltaV, 0);
int transformatVolume = (int) (mCurrentVolume * 100 / max + deltaY * 100 / screenHeight);
pb_volume.setProgress(transformatVolume);
fl_volume.setVisibility(VISIBLE);
}

这里的思路很简单,将播放器的View实现OnTouchListener,在onTouch中监听MotionEvent的ACTION_DOWN、ACTION_MOVE、ACTION_UP事件,然后做相应的处理,代码里有注释,这里就不多说了

因为播放器控件是我们自定义的,当时进度比较赶,所以播放进度和音量的显示的布局就直接和播放器放在一起了,后面可以考虑分装成Dialog的形式,将其抽离出来

参考

http://www.cnblogs.com/xitang/archive/2013/06/22/3150380.html