问题
I have an application that makes a list of videos with play button. When I click on the play button, a separate activity is started using intent. I just want that when the video playback is finished, the activity should automatically finish itself and go back to the main Activity. Here is my code for creating videoview.
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
Intent i = getIntent();
Bundle extras = i.getExtras();
filename = extras.getString("videofilename");
mVideoView = (VideoView)findViewById(R.id.videoview);
path=filename;
if (path == "") {
Toast.makeText(
ViewVideo.this,
"no video selected,
Toast.LENGTH_LONG).show();
} else {
mVideoView.setVideoPath(path);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
mVideoView.start();
}
}
any suggestions???
回答1:
Register an OnCompletionListener to the videoView, in the listener implement the call to finish()
.
Edit (to answer to comment):
use the method setOnCompletionListener:
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion (MediaPlayer mp) {
// your code to clean up and finish the activity...
}
});
回答2:
You can set a MediaPlayer.OnCompletionListener
on the VideoView
using VideoView.setOnCompletionListener
, you'll then be able to finish the containing activity when the video finishes playing.
来源:https://stackoverflow.com/questions/6681539/how-to-go-back-after-playing-video