Listener (or handler) for video finish
-
13-09-2019 - |
Question
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.
OTHER TIPS
here is my working chunk of code:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
// Do whatever u need to do here
}
});
vidView=(VideoView)findViewById(R.id.vidView);
vidView.setMediaController(null);
vidView.setVideoPath( "/mnt/external_sd/somerandommovie.3gp" );
vidView.start();
vidView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
vidView.start();
}
});
I found that the listener was firing, the issue was mp.start() didn't seem to do anything, so calling start again on the original object seems to be working fine.
in your onCompletion listener, try using videoView.start(); instead of mp.start();
Make sure you are using the player on the main thread, it seems the callback only works on the main thread.