Search code examples
androidmedia-player

Listener (or handler) for video finish


I have implement the following code in order to test playing a video from a remote web server through it´s URL.

videoView = (VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse("http://sayedhashimi.com/downloads/android/movie.mp4"));
videoView.requestFocus();

The code is working just fine, even in the Android emulator. I just would like to know if there's any listener (or handler) to detect the finish of the video that is being reproduced?

Update:

I have the following code now:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.player);
        
    videoView = (VideoView)this.findViewById(R.id.videoView);
    playVideo();
        
    // video finish listener
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {        
        @Override
        public void onCompletion(MediaPlayer mp) {
            // not playVideo
            // playVideo();
            mp.start();
        }
    });
}

public void playVideo() {
    MediaController mc = new MediaController(this);
    videoView.setMediaController(mc);
    videoView.setVideoURI(Uri.parse("http://sayedhashimi.com/downloads/android/movie.mp4"));
    videoView.requestFocus(); 
}

When the activity is just called, the video works fine but when the video finishes, I would like that it played again, which it's not occurring.


Solution

  • Seems you are looking for

    setOnCompletionListener(MediaPlayer.OnCompletionListener l)
    

    More in depth explanation can be found here

    EDIT

    This shows a solution where playback is called after completion using VideoView, MediaController and setOnCompletionListener().