1.seekTo()的使用

有一个重新进入跳转到上次播放视频地方的需求,其实就是记住播放位置,重新进入时直接在对应位置播放。
seekTo()方法播放网络视频是有一定延时的,所以要在视频缓冲好后才进行播放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
videoView.seekTo(position);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
videoView.start();
}
});
}
});

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
25
26
27
28
29
30
31
32
33
34
35
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
if(player.isPlaying()){
player.pause();
}else {
player.start();
}
return true;
case KeyEvent.KEYCODE_DPAD_DOWN_LEFT:
return true;
case KeyEvent.KEYCODE_DPAD_DOWN_RIGHT:
return true;
case KeyEvent.KEYCODE_DPAD_UP:
player.showControls();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
player.hideControls();
return true;
}
}
return super.onKeyDown(keyCode, event);
}