How to play videos one after another sequentially?

前端 未结 2 1927
迷失自我
迷失自我 2021-01-24 07:38

I need to play videos one after the another sequentially.I tried searching in google but dint get any answer.Please help.Its bit urgent.

This is my code



        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-24 08:23

    private int[] ids = {R.raw.vid1, R.raw.vid2, R.raw.vid3, R.raw.vid4, R.raw.vid5};
    private int index = 1;
    private VideoView myVideo1;
    

    OnCreate

    //Video Field
        myVideo1 = (VideoView) findViewById(R.id.myvideoview);
        myVideo1.requestFocus();
        myVideo1.setVideoURI(getPath(index));
        index++;
    
        myVideo1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                myVideo1.start();
            }
        });
    
        myVideo1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                if (index == ids.length) index = 1;
                index++;
                myVideo1.setVideoURI(getPath(index));
            }
        });
    
        private Uri getPath(int id) {
        return Uri.parse("android.resource://" + getPackageName() + "/raw/vid" + id);
    }
    

    We asume that your videos are on raw folder and with name vid1 vid2 etc..

提交回复
热议问题