服务器之家

服务器之家 > 正文

基于VideoView自定义控制面板的视频播放器

时间:2022-03-08 15:57     来源/作者:尽人事看天意

本文实例为大家分享了VideoView原生自定义视频播放器的具体代码,供大家参考,具体内容如下

项目用到的技术点和亮点

  • VideoView封装视频播放工具类
  • 自定义视频控制面板
  • 手势识别器的使用

1. VideoView封装视频播放工具类

视频播放这些其实都挺简单的,VideoView这个类都提供了相应方法,详细见代码 代码都比较简单,不过需要注意的是要在主线程中更新视频面板的UI信息。

?
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/********************************************************
 * 视频业务类
 * 最后修改时间:2017/9/23
 * @author zlc
 *
 *********************************************************/
package com.zlc.video;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;
public class VideoBusiness implements MediaPlayer.OnPreparedListener,MediaPlayer.OnCompletionListener,MediaPlayer.OnErrorListener{
 
 private Activity activity;
 private WakeLock mWakeLock;
 public VideoView mVideoView;
 private VideoController mController;
 
 /**播放状态枚举,有三种播放状态:空闲,正在准备*/
 private enum PLAYER_STATUS {
  IDLE, PREPARING,PAUSED, PREPARED,RESUMED,STOPPED
 }
 /**当前播放状态*/
 public PLAYER_STATUS mPlayerStatus = PLAYER_STATUS.IDLE;
 
 /**播放信息异步处理方法,用于更新进度*/
 /**事件标志*/
 private int mLastPos;
 
 public VideoBusiness(Activity activity){
  this.activity = activity;
  // 保持屏幕高亮
  PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
  mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "Test");
 }
 
 //初始化视频播放器
 public void initVideo(VideoView videoView,VideoController controller,String sourceUrl){
 
  this.mVideoView = videoView;
  this.mController = controller;
  mController.setVideoBusiness(this);
  Log.e("msg","设置播放地址 = "+sourceUrl);
  mVideoView.setOnPreparedListener(this);
  mVideoView.setOnCompletionListener(this);
  mVideoView.setOnErrorListener(this);
  mVideoView.setVideoPath(sourceUrl); //设置播放地址
 
 }
 
 //开始播放
 public void startPlay(){
  if (null != mWakeLock && (!mWakeLock.isHeld())) {
   mWakeLock.acquire();
  }
  if(null != mVideoView) {
   Log.e("msg", "播放");
   mVideoView.start();
   mPlayerStatus = PLAYER_STATUS.PREPARING;
  }
 }
 
 
 /**
  * 暂停播放
  */
 public void pause() {
  if (null != mWakeLock) {
   mWakeLock.release();
  }
  if(null != mVideoView && mVideoView.isPlaying()){
   mVideoView.pause();
   mPlayerStatus = PLAYER_STATUS.PAUSED;
   mLastPos = getCurrentTime();
  }
 }
 
 /**
  * 继续播放
  */
 public void resume(){
  if (null != mWakeLock) {
   mWakeLock.acquire();
  }
  if(null != mVideoView){
   //mVideoView.resume();
   mVideoView.seekTo(mLastPos);
   mVideoView.start();
   mPlayerStatus = PLAYER_STATUS.RESUMED;
  }
 }
 
 /**
  * 停止播放
  */
 public void stop() {
 
  if (null != mWakeLock) {
   mWakeLock.release();
  }
  if(null != mVideoView){
   mLastPos = getCurrentTime();
   mVideoView.stopPlayback();
   mPlayerStatus = PLAYER_STATUS.STOPPED;
  }
 }
 
 /**
  * 判断是否正在播放
  * @return
  */
 public boolean isPlaying(){
  return mVideoView!=null && mVideoView.isPlaying();
 }
 
 /**
  * 是否暂停
  */
 public boolean isPause(){
  return mPlayerStatus == PLAYER_STATUS.PAUSED;
 }
 
 /**
  * 释放资源
  */
 public void release(){
  if (null != mWakeLock) {
   mWakeLock.release();
   mWakeLock = null;
  }
  if(null != mVideoView){
   mVideoView.stopPlayback();
   mVideoView = null;
  }
 }
 
 @Override
 public void onCompletion(MediaPlayer mediaPlayer) {
  Log.e("onCompletion","视频播放完了");
  mController.showLong();
  mController.setProgress(0);
  mLastPos = 0;
  mPlayerStatus = PLAYER_STATUS.IDLE;
  removeUIMessage();
 }
 
 @Override
 public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
  Log.e("onError","视频播放报错了");
  return false;
 }
 
 @Override
 public void onPrepared(MediaPlayer mediaPlayer) {
  Log.e("onPrepared","视频准备好了");
  if (mPlayerStatus!= PLAYER_STATUS.PAUSED){
   int totalTime = getTotalTime();
   mController.setTotalTime(totalTime);
   mController.setProgress(0);
   mController.setMaxProgress(totalTime);
   mPlayerStatus = PLAYER_STATUS.PREPARED;
   sendUIMessage();
  }
 }
 
 /**
  * 进度条拖拽播放
  * @param time
  */
 public void seekToPlay(int time){
  int totalSecond = getTotalTime();
  int tempTime = time > totalSecond ? totalSecond : time;
  mVideoView.seekTo(tempTime);
  sendUIMessage();
 }
 
 
 //视频暂停播放 播放大按钮点击事件
 public void playVideo(ImageView id_btn_video_play, ImageView img){
  if(isPlaying()){
   pause();
   id_btn_video_play.setVisibility(View.VISIBLE);
   img.setImageResource(R.drawable.video_pause);
   mPlayerStatus = PLAYER_STATUS.PAUSED;
   mUIHandler.sendEmptyMessageDelayed(UI_EVENT_UPDATE_CURRPOSITION, 500);
  }else if(isPause()){
   resume();
   id_btn_video_play.setVisibility(View.GONE);
   img.setImageResource(R.drawable.video_play);
   mPlayerStatus = PLAYER_STATUS.RESUMED;
  }else{
   img.setImageResource(R.drawable.video_play);
   id_btn_video_play.setVisibility(View.GONE);
   startPlay();
   mPlayerStatus = PLAYER_STATUS.PREPARING;
  }
 }
 
 private boolean isCurrentLandscape = false; //是不是横屏
 //横竖屏切换按钮点击方法
 public void toggleScreenDir(View v){
  if (isCurrentLandscape) {// 如果当前是横屏,则切换为竖屏,然后把按钮为变为变大的图标
   activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
   if(v instanceof ImageView){
    ((ImageView)v).setImageResource(R.drawable.zuidahua_2x);
   }
  } else {// 如果当前是竖屏,则切换为横屏,然后把按钮为变为变小的图标
   activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
   if(v instanceof ImageView){
    ((ImageView)v).setImageResource(R.drawable.xiaohua_2x);
   }
  }
  isCurrentLandscape = !isCurrentLandscape;
 }
 
 public UIHandler mUIHandler = new UIHandler(Looper.getMainLooper());
 public final int UI_EVENT_UPDATE_CURRPOSITION = 1; //更新进度信息
 public boolean isSeekBarEnable = true;
 
 class UIHandler extends Handler{
  public UIHandler(Looper mainLooper) {
   super(mainLooper);
  }
 
  @Override
  public void handleMessage(Message msg) {
   switch (msg.what) {
    //更新进度及时间
    case UI_EVENT_UPDATE_CURRPOSITION:
     if (isSeekBarEnable) {
      int currentPosition = getCurrentTime();
      String timeString = mController.getTimeString(currentPosition);
      //Log.e("handleMessage",timeString);
      if(isPlaying()) {
       mController.setProgress(currentPosition);
       mUIHandler.sendEmptyMessageDelayed(
         UI_EVENT_UPDATE_CURRPOSITION, 200);
      }
     }
     break;
   }
  }
 }
 
 public void sendUIMessage(){
  mUIHandler.sendEmptyMessage(UI_EVENT_UPDATE_CURRPOSITION);
 }
 
 public void removeUIMessage(){
  mUIHandler.removeMessages(UI_EVENT_UPDATE_CURRPOSITION);
 }
 
 //获取视频总时间
 public int getTotalTime(){
  return mVideoView==null ? 0 : mVideoView.getDuration();
 }
 
 //获取视频当前时间
 public int getCurrentTime(){
  return mVideoView==null ? 0 : mVideoView.getCurrentPosition();
 }
}

2. 手势识别器的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//1. 创建手势识别器
 progressGestureDetector = new GestureDetector(mContext,new ProgressGestureListenr());
 
//2.复写GestureDetector.OnGestureListener的6个方法
boolean onDown(MotionEvent e) //用户按下屏幕就会触发
 
void onShowPress(MotionEvent e) ////down事件发生而move或则up还没发生前触发该事件
 
boolean onSingleTapUp(MotionEvent e) //一次点击up事件
 
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) //在屏幕上拖动事件
 
void onLongPress(MotionEvent e) //长按事件
 
 boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) //滑动手势事件
 
 //3 onTouch中拦截
 @Override
 public boolean onTouch(View view, MotionEvent event) {
 
  return progressGestureDetector.onTouchEvent(event);
 }
 //4 事件绑定
 progressArea.setOnTouchListener(this);

3. 自定义视频控制面板

代码比较简单同时代码不长 都有注释 需要的可以参考下 大神勿喷

 

?
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package com.zlc.video;
 
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
 
import com.zlc.video.utils.DensityUtil;
import com.zlc.video.utils.ScreenUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class VideoController extends RelativeLayout implements View.OnTouchListener,View.OnClickListener,SeekBar.OnSeekBarChangeListener{
 
 private Context mContext;
 private View mContainer;
 private VideoBusiness videoBusiness;
 /** 表示当前视频控制面板是否展示*/
 public boolean isShow = true;
 private Handler hideHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   if (isShow) {
    hideController();
   }
  }
 };
 /** 视频功能控制底边栏*/
 private LinearLayout mMediaController;
 
 /***************手势相关*********************/
 private int GESTURE_FLAG = 0;//1调节进度,2调节音量,3调节亮度
 private FrameLayout mProgressIndicator;
 private ProgressBar progressBar;
 /**进度相关*/
 private GestureDetector progressGestureDetector;
 private static final int GESTURE_MODIFY_PROGRESS = 1;
 /**音量相关*/
 private static final int GESTURE_MODIFY_VOLUME = 2;
 private AudioManager audiomanager;
 private int maxVolume, currentVolume;
 /**亮度相关*/
 private static final int GESTURE_MODIFY_BRIGHTNESS = 3;
 private WindowManager.LayoutParams brightnessLp;
 private int maxBrightness,currentBrightness;
 private LinearLayout progressArea;
 private int targetTime;
 //UI相关
 private RelativeLayout id_rl_video_controller;
 private FrameLayout id_fl_video_play;
 private FrameLayout id_fl_video_expand;
 private TextView id_video_time;
 private TextView id_video_totaltime;
 private SeekBar id_sb_progress;
 private ImageView id_btn_video_play;
 private ImageView id_iv_video_play;
 private TextView id_tv_video_info;
 
 
 public VideoController(Context context) {
  this(context, null);
 }
 
 public VideoController(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }
 
 public VideoController(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  mContext = context;
  init();
  initListener();
 }
 
 private void init() {
  //初始化音量相关
  audiomanager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
  maxVolume = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量
  currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
  //初始化亮度相关
  brightnessLp=((Activity)mContext).getWindow().getAttributes();
  currentBrightness = getCurrentBrightness();
  maxBrightness = 255; //设置最大亮度
  initView();
 }
 
 private void initView() {
  mContainer = View.inflate(mContext, R.layout.video_controller, null);
  id_rl_video_controller = findView(R.id.id_rl_video_controller);
  mMediaController = findView(R.id.id_ll_controller);
  mProgressIndicator = findView(R.id.id_fl_progress_indicator);
  progressBar = findView(R.id.id_pb_gesture_progress);
  progressArea = findView(R.id.id_ll_video_gesture_progress);
  id_fl_video_play = findView(R.id.id_fl_video_play);
  id_fl_video_expand = findView(R.id.id_fl_video_expand);
  id_video_time = findView(R.id.id_video_time);
  id_video_totaltime = findView(R.id.id_video_totaltime);
  id_sb_progress = findView(R.id.id_sb_progress);
  id_btn_video_play = findView(R.id.id_btn_video_play);
  id_iv_video_play = findView(R.id.id_iv_video_play);
  id_tv_video_info = findView(R.id.id_tv_video_info);
  addView(mContainer);
 }
 
 private <T extends View> T findView(int viewId) {
  return (T) mContainer.findViewById(viewId);
 }
 
 public void setVideoBusiness(VideoBusiness videoBusiness) {
  this.videoBusiness = videoBusiness;
 }
 
 private void initListener() {
  //进度手势相关
  progressGestureDetector = new GestureDetector(mContext,new ProgressGestureListenr());
  progressGestureDetector.setIsLongpressEnabled(true);
  progressArea.setLongClickable(true);
  progressArea.setOnTouchListener(this);
  mMediaController.setOnClickListener(this);
  id_rl_video_controller.setOnClickListener(this);
  id_fl_video_play.setOnClickListener(this);
  id_fl_video_expand.setOnClickListener(this);
  id_btn_video_play.setOnClickListener(this);
  id_sb_progress.setOnSeekBarChangeListener(this);
 }
 
 //状态切换
 public void toggle() {
  if (isShow) {
   hideController();
  } else {
   showController();
  }
 }
 
 //隐藏底部控制栏
 public void hideController() {
  isShow = false;
  mMediaController.setVisibility(View.GONE);
  endTimer();
 }
 
 //显示底部控制栏
 public void showController() {
  isShow = true;
  mMediaController.setVisibility(View.VISIBLE);
  startTimer();
 }
 
 private void startTimer() {
  if (hideHandler != null){
   hideHandler.removeMessages(0);
  }
  hideHandler.sendEmptyMessageDelayed(0, 5000);
 }
 
 private void endTimer() {
  hideHandler.removeMessages(0);
 }
 
 public void resetTimer() {
  endTimer();
  startTimer();
 }
 
 public void showLong() {
  isShow = true;
  mMediaController.setVisibility(View.VISIBLE);
 }
 
 //设置视频总时间
 public void setTotalTime(int time) {
  String totalTime = getTimeString(time);
  id_video_totaltime.setText(totalTime);
 }
 
 //设置视频当前进度
 public void setProgress(int progress){
  int maxProgress = videoBusiness.getTotalTime();
  int tempProgress = progress > maxProgress ? maxProgress : progress;
  id_sb_progress.setProgress(tempProgress);
 }
 
 //是指视频总进度
 public void setMaxProgress(int maxProgress){
  id_sb_progress.setMax(maxProgress);
 }
 
 //获取当前亮度
 private int getCurrentBrightness(){
  int currentBrightness = 255;
  if (brightnessLp.screenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE){
   // 获取系统亮度
   try {
    currentBrightness = Settings.System.getInt(((Activity)mContext).getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
   } catch (Settings.SettingNotFoundException e) {
    e.printStackTrace();
   }
  }else{
   // 获取当前窗口亮度
   currentBrightness = (int)(brightnessLp.screenBrightness * 255);
  }
  return currentBrightness;
 }
 
 
 @Override
 public void onClick(View view) {
  switch (view.getId()){
   case R.id.id_ll_controller: //底部控制器
    showController();
    break;
   case R.id.id_rl_video_controller: //全屏点击
    toggle();
    break;
   case R.id.id_fl_video_play: // 暂停/播放
   case R.id.id_btn_video_play: // 暂停/播放
    videoBusiness.playVideo(id_btn_video_play,id_iv_video_play);
    break;
   case R.id.id_fl_video_expand: //全屏
    resetTimer();
    videoBusiness.toggleScreenDir(view);
    break;
  }
 }
 
 
 @Override
 public boolean onTouch(View view, MotionEvent event) {
 
  getParent().requestDisallowInterceptTouchEvent(true);
  if (event.getAction() == MotionEvent.ACTION_UP) {
   mProgressIndicator.setVisibility(View.INVISIBLE);
   if (GESTURE_FLAG == GESTURE_MODIFY_PROGRESS) { //调节进度
    Log.e("进度时间","targetTime="+targetTime);
    videoBusiness.seekToPlay(targetTime);
    videoBusiness.isSeekBarEnable = true;
    hideController();
   }
   GESTURE_FLAG = 0;// 手指离开屏幕后,重置调节音量或进度的标志
  }
  return progressGestureDetector.onTouchEvent(event);
 }
 
 private int currentPosition; //当前播放进度
 private int totalPosition; //总播放进度
 class ProgressGestureListenr implements GestureDetector.OnGestureListener{
  private boolean firstScroll = false;// 每次触摸屏幕后,第一次scroll的标志
  private int slop;// 触发设置变动的最小距离
  @Override
  public boolean onDown(MotionEvent e) { //用户按下屏幕就会触发
   //初始数据
   slop = DensityUtil.dp2px(mContext,2);
   currentPosition = videoBusiness.getCurrentTime();
   totalPosition = videoBusiness.getTotalTime();
   firstScroll = true;
   return false;
  }
 
  @Override
  public void onShowPress(MotionEvent e) { //down事件发生而move或则up还没发生前触发该事件
  }
 
  @Override
  public boolean onSingleTapUp(MotionEvent e) { //一次点击up事件
   toggle();
   if(videoBusiness.isPlaying()){ //正在播放
    return false;
   }else{ //暂停或者开始播放
    videoBusiness.playVideo(id_btn_video_play,id_iv_video_play);
    isShow = false;
    return true;
   }
  }
 
  //在屏幕上拖动事件
  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
   if (firstScroll) {// 以触摸屏幕后第一次滑动为标准,避免在屏幕上操作切换混乱
    // 横向的距离变化大则调整进度,纵向的变化大则调整音量
    Log.e("xxxxxxx",e1.getX()+"");
    Log.e("yyyyyyy",e1.getY()+"");
    setScroll(e1, distanceX, distanceY);
   }
   // 如果每次触摸屏幕后第一次scroll是调节进度,那之后的scroll事件都处理音量进度,直到离开屏幕执行下一次操作
   switch (GESTURE_FLAG){
    case GESTURE_MODIFY_PROGRESS: //调节当前进度
     setCurrentProgress(distanceX, distanceY,slop);
     break;
    case GESTURE_MODIFY_VOLUME: //调节当前音量
     setCurrentVolume(distanceX, distanceY,slop);
     break;
    case GESTURE_MODIFY_BRIGHTNESS: //调节当前亮度
     setCurrentBrightess(distanceX, distanceY,slop);
     break;
   }
   firstScroll=false;
   return false;
  }
 
  @Override
  public void onLongPress(MotionEvent e) { //长按事件
  }
 
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //滑动手势事件
   return false;
  }
 }
 
 //滑动事件
 private void setScroll(MotionEvent e1, float distanceX, float distanceY) {
  int screenWidth = ScreenUtil.getScreenWidth(mContext);
  Log.e("屏宽",screenWidth+"");
  //横向的距离变化大则调整进度,纵向的变化大则调整音量
  mProgressIndicator.setVisibility(View.VISIBLE);
  if (Math.abs(distanceX) >= Math.abs(distanceY)) { //调整进度
   progressBar.setVisibility(View.INVISIBLE);
   id_tv_video_info.setVisibility(VISIBLE);
   GESTURE_FLAG = GESTURE_MODIFY_PROGRESS;
   videoBusiness.isSeekBarEnable = false;
   endTimer();
   showLong();
  }else { //调整音量
   progressBar.setVisibility(VISIBLE);
   id_tv_video_info.setVisibility(INVISIBLE);
   if (e1.getX() > screenWidth / 2){ //屏幕右边上下滑动调整音量
    //右半屏音量
    setVideoVolume();
   }else{        //屏幕左边上下滑动调整亮度
    //左半屏亮度
    setVideoBrightness();
   }
  }
 }
 
 //设置当前进度
 private void setCurrentProgress(float distanceX, float distanceY,float slop) {
  if (Math.abs(distanceX) > Math.abs(distanceY)) {// 横向移动大于纵向移动
   //Log.e("setCurrentProgress",distanceX+"");
   if(distanceX >= slop){ //从右往左滑 快退
    mProgressIndicator.setBackgroundResource(R.drawable.kuaitui);
    if (currentPosition > 1000) {
     currentPosition -= 1500;
    }
   }else if(distanceX <= -slop){ //从左往右滑 快进
    mProgressIndicator.setBackgroundResource(R.drawable.kuaijin);
    if (currentPosition < totalPosition) {
     currentPosition += 1500;
    }
   }
  }
  targetTime = currentPosition;
  Log.e("进度时间","currentPosition="+currentPosition);
  id_sb_progress.setProgress(currentPosition);
  id_video_time.setText(getTimeString(currentPosition));
  String videoPbInfo = getTimeString(currentPosition)+"/"+ getTimeString(totalPosition);
  id_tv_video_info.setText(videoPbInfo);
 }
 
 //设置当前亮度
 private void setCurrentBrightess(float distanceX, float distanceY, float slop) {
  currentBrightness = getCurrentBrightness();
  if (Math.abs(distanceY) > Math.abs(distanceX)) {// 纵向移动大于横向移动
   if (distanceY >= slop) {// 上滑 亮度调大,注意横屏时的坐标体系,尽管左上角是原点,但横向向上滑动时distanceY为正
    if (currentBrightness < maxBrightness) {// 为避免调节过快,distanceY应大于一个设定值
     currentBrightness += 8;
    }
   } else if (distanceY <= -slop) {// 亮度调小
    if (currentBrightness > 0) {
     currentBrightness -= 8;
    }
    if (currentBrightness<0){
     currentBrightness=0;
    }
   }
   mProgressIndicator.setBackgroundResource(R.drawable.liangdu);
   progressBar.setProgress(currentBrightness);
   changeAppBrightness(mContext,currentBrightness);
  }
 }
 
 //设置当前音量
 private void setCurrentVolume(float distanceX, float distanceY,float slop) {
 
  currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
  if (Math.abs(distanceY) > Math.abs(distanceX)) { // 纵向移动大于横向移动
   if (distanceY >= slop) { // 上滑 音量调大,注意横屏时的坐标体系,尽管左上角是原点,但横向向上滑动时distanceY为正
    if (currentVolume < maxVolume) {// 为避免调节过快,distanceY应大于一个设定值
     currentVolume++;
    }
    mProgressIndicator.setBackgroundResource(R.drawable.yinliang);
   } else if (distanceY <= -slop) {// 音量调小 下滑
    if (currentVolume > 0) {
     currentVolume--;
     if (currentVolume == 0) {// 静音,设定静音独有的图片
      mProgressIndicator.setBackgroundResource(R.drawable.jingying);
     }
    }
   }
   progressBar.setProgress(currentVolume);
   audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
  }
 }
 
 //设置视频亮度
 private void setVideoBrightness() {
  progressBar.setMax(maxBrightness);
  progressBar.setProgress(currentBrightness);
  mProgressIndicator.setBackgroundResource(R.drawable.liangdu);
  GESTURE_FLAG = GESTURE_MODIFY_BRIGHTNESS;
 }
 
 //设置视频音量
 private void setVideoVolume() {
  progressBar.setMax(maxVolume);
  progressBar.setProgress(currentVolume);
  mProgressIndicator.setBackgroundResource(R.drawable.yinliang);
  GESTURE_FLAG = GESTURE_MODIFY_VOLUME;
 }
 
 //改变系统亮度
 public void changeAppBrightness(Context context, int brightness) {
  Window window = ((Activity) context).getWindow();
  WindowManager.LayoutParams lp = window.getAttributes();
  if (brightness == -1) {
   lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
  } else {
   lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
  }
  window.setAttributes(lp);
 }
 
 public String getTimeString(int second) {
  int temp = second / 1000;
  int hh = temp / 3600;
  SimpleDateFormat sdf;
  if (0 != hh) {
   sdf = new SimpleDateFormat("HH:mm:ss");
  } else {
   sdf = new SimpleDateFormat("mm:ss");
  }
  String format = sdf.format(new Date(second));
  return format;
 }
 
 //进度条改变
 @Override
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  String timeString = getTimeString(progress);
  id_video_time.setText(timeString);
 }
 
 //开始拖动
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  showLong();
  videoBusiness.removeUIMessage();
 }
 
 //结束拖动
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  showController();
  int progress = seekBar.getProgress();
  videoBusiness.seekToPlay(progress);
 }
 
}

4.下载地址

VideoView原生自定义视频播放器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/rjgcszlc/article/details/78073955

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部