问题
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.
回答1:
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().
回答2:
here is my working chunk of code:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
// Do whatever u need to do here
}
});
回答3:
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.
回答4:
in your onCompletion listener, try using videoView.start(); instead of mp.start();
回答5:
Make sure you are using the player on the main thread, it seems the callback only works on the main thread.
来源:https://stackoverflow.com/questions/1847818/listener-or-handler-for-video-finish