Android VideoView - How to play videos in sequence

前端 未结 2 1963
野性不改
野性不改 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.:)

    0 讨论(0)
  • 2021-01-05 10:38

    Try something like that - it works perfect for me.

    public class MainActivity extends Activity {
    
    private VideoView videoView = null;
    String[] videoArray = {"video1", "video2"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[0]);
    
        videoView = (VideoView)findViewById(R.id.videoView);
        videoView.setVideoURI(videoUri);
        videoView.start();
    
        videoView.setOnCompletionListener(new OnCompletionListener() {
    
            @Override
            public void onCompletion(MediaPlayer mp) 
            {
                Uri videoUri = Uri.parse("android.resource://" + MainActivity.this.getPackageName() + "/raw/" + videoArray[1]);
                videoView.setVideoURI(videoUri);
                videoView.start();
            }
        });
    }
    }
    
    0 讨论(0)
提交回复
热议问题