Android VideoView - How to play videos in sequence

前端 未结 2 1964
野性不改
野性不改 2021-01-05 10:11

I\'m trying to develop an android application that plays more than one video in one videoview. When one is finished, the second has to start and so on.

My videos are

2条回答
  •  臣服心动
    2021-01-05 10:28

    Well you are getting error because you are trying to initialize videoview again before reseting it. Make a change to your code like this.

    myVideoView.setOnCompletionListener(new   MediaPlayer.OnCompletionListener() {
    
            @Override
            public void onCompletion(MediaPlayer mp) {
    
                startOtherVid();
            }
    });
    

    Now make method startOtherVid() and initialize videoview here after releasing the previous one.

    private void startOtherVid(){
      myVideoView.stopPlayback();
      videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" 
                            + videoNames.get(VideoI));
      myVideoView.setVideoURI(videoUri);
    
      myVideoView.start();
      .... 
       .....
    }
    

    This way you will release one videoview object and create again. There will be a short time to load, but you can handle it visually.

    Edit

    You can also release mediaplayer object and solve your problem.

    public void onCompletion(MediaPlayer mp) {
    
    try {
        mp.reset();
        videoUri = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" 
                            + videoNames.get(VideoI));
        myVideoView.setVideoURI(videoUri);
    
        myVideoView.start();
    }
    catch(Exception e){e.printstacktrace();}
    });
    

    Cheers.:)

提交回复
热议问题