亲宝软件园·资讯

展开

VideoView实现视频无缝连续播放 VideoView实现视频无缝连续播放

卡夫卡的DOVE 人气:0

该视频播放器采用了较为简单的videoview,基本思想就是用setOnCompletionListener对视频播进行监听,当视频播放完毕之后,播放下一段视频,视频源放在SD卡内的Download文件夹内,具体代码如下:

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();}


      }

    });
  }


}

加载全部内容

相关教程
猜你喜欢
用户评论