How to go back after playing video

荒凉一梦 提交于 2019-12-13 01:25:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!