How to play videos one after another sequentially?

前端 未结 2 1928
迷失自我
迷失自我 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 07:56

    Try this:

    videoView = (VideoView) findViewById(R.id.v);
    setup();
    videoView.setOnCompletionListener(completionListener);
    

    setup Function:

    public void setup() {
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.test);
            videoView.setVideoURI(uri);
            videoView.start();
        }
    

    implement onCompletionListener as:

    OnCompletionListener completionListener=new OnCompletionListener() {
    
            public void onCompletion(MediaPlayer mp) {
                mp.stop();
                setup();
            }
        };
    
    0 讨论(0)
  • 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..

    0 讨论(0)
提交回复
热议问题