该视频播放器采用了较为简单的videoview,基本思想就是用setOnCompletionListener对视频播进行监听,当视频播放完毕之后,播放下一段视频,视频源放在SD卡内的Download文件夹内,具体代码如下:
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
|
public class MainActivity extends Activity { VideoView videoView; static int pos= 1 ; //静态整型变量用于标记播放到了第几段视频 MediaController mController; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.activity_main); videoView=(VideoView)findViewById(R.id.video); mController= new MediaController( this ); File video= new File( "/mnt/sdcard/Download/video" +pos+ ".mp4" ); if (video.exists()) { videoView.setVideoPath(video.getAbsolutePath()); /* videoView.setMediaController(mController); mController.setMediaPlayer(videoView);*/ videoView.requestFocus(); videoView.start(); } videoView.setOnCompletionListener( new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub pos=pos+ 1 ; //在这里将视频地址变为下一个 File video2= new File( "/mnt/sdcard/Download/video" +pos+ ".mp4" ); if (video2.exists()) { try { videoView.setVideoPath(video2.getAbsolutePath()); /*videoView.setMediaController(mController); mController.setMediaPlayer(videoView);*/ videoView.requestFocus(); videoView.start(); } catch (Exception e) { e.printStackTrace(); } } else {Toast.makeText(MainActivity. this , "视频播放完毕" ,Toast.LENGTH_SHORT).show();} } }); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u013555975/article/details/50724347