1 packagecom.himi.videoplayer;2
3 importjava.util.Timer;4 importjava.util.TimerTask;5
6 importandroid.app.Activity;7 importandroid.os.Bundle;8 importandroid.view.MotionEvent;9 importandroid.view.View;10 importandroid.view.animation.Animation;11 importandroid.view.animation.TranslateAnimation;12 importandroid.widget.LinearLayout;13 importandroid.widget.SeekBar;14 importandroid.widget.SeekBar.OnSeekBarChangeListener;15 importandroid.widget.VideoView;16
17 public class MainActivity extendsActivity {18 privateSeekBar seekBar1;19 privateLinearLayout ll_controller;20 privateVideoView vv;21 private Timer timer;//计时器
22 privateTimerTask task;//计时器任务23
24 @Override25 protected voidonCreate(Bundle savedInstanceState) {26 super.onCreate(savedInstanceState);27 setContentView(R.layout.activity_main);28
29 vv =(VideoView) findViewById(R.id.vv);30 ll_controller =(LinearLayout) findViewById(R.id.ll_controller);31 seekBar1 =(SeekBar) findViewById(R.id.seekBar1);32 seekBar1.setOnSeekBarChangeListener(newOnSeekBarChangeListener() {33
34 public voidonStopTrackingTouch(SeekBar seekBar) {35 vv.seekTo(seekBar.getProgress());36 }37
38 public voidonStartTrackingTouch(SeekBar seekBar) {39
40 }41
42 public void onProgressChanged(SeekBar seekBar, intprogress,43 booleanfromUser) {44
45 }46 });47
48 vv.setVideoPath("http://49.123.72.40:8080/oppo.mp4");49 vv.start();50
51 timer = newTimer();52 task = newTimerTask() {53
54 @Override55 public voidrun() {56 //每隔一秒执行下run方法
57
58 int max = vv.getDuration();//获取视频的总时长
59 seekBar1.setMax(max);60
61 int process =vv.getCurrentPosition();62 seekBar1.setProgress(process);63
64 }65 };66 timer.schedule(task, 0, 1000);//第二参数为表示等待多长时间,第一次启动任务;这里为0,表示马上启动67 }68
69 @Override70 protected voidonDestroy() {71 timer.cancel();72 task.cancel();73 timer = null;74 task = null;75 super.onDestroy();76 }77
78 @Override79 public booleanonTouchEvent(MotionEvent event) {80
81 if (event.getAction() ==MotionEvent.ACTION_DOWN) {82
83 if (ll_controller.getVisibility() ==View.VISIBLE) {84
85 ll_controller.setVisibility(View.INVISIBLE);//隐藏
86 TranslateAnimation ta = newTranslateAnimation(87 Animation.RELATIVE_TO_SELF, 0,88 Animation.RELATIVE_TO_SELF, 0,89 Animation.RELATIVE_TO_SELF, 0,90 Animation.RELATIVE_TO_SELF, 1.0f);91 ta.setDuration(200);92 ll_controller.startAnimation(ta);93 } else if (ll_controller.getVisibility() ==View.INVISIBLE) {94
95 ll_controller.setVisibility(View.VISIBLE);//显示ll_controller.setVisibility(View.INVISIBLE);//隐藏
96 final TranslateAnimation ta = newTranslateAnimation(97 Animation.RELATIVE_TO_SELF, 0,98 Animation.RELATIVE_TO_SELF, 0,99 Animation.RELATIVE_TO_SELF, 1.0f,100 Animation.RELATIVE_TO_SELF, 0);101 ta.setDuration(200);102 ll_controller.startAnimation(ta);103
104 newThread() {105 public voidrun() {106 try{107 Thread.sleep(2000);108 } catch(InterruptedException e) {109 //TODO 自动生成的 catch 块
110 e.printStackTrace();111 }112 runOnUiThread(newRunnable() {113
114 public voidrun() {115 ll_controller.startAnimation(ta);116
117 }118 });119 };120 }.start();121
122 }123
124 }125 return super.onTouchEvent(event);126 }127
128 }