视频播放结束后播放视频并退出应用的活动

时间:2018-07-03 21:39:07

标签: java android main-activity

在过去的18个月中,我一直在android studio中构建简单的应用程序,但我真的很想尝试解决这个问题,因此任何帮助都将很棒

到目前为止,我的应用程序运行良好,请在第一个活动中打开该应用程序,然后按播放按钮,然后第二个活动将打开并播放视频,在视频结束时,它会返回到第一个活动,但是我正在尝试退出应用程序

猜我应该可以添加

public void onCompletion(MediaPlayer player) {      
onStop();
onDestroy();

}






 public class PlayVideo extends Activity {

    boolean videoPlayed ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);


        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );

        videoPlayed = true;
        playvideo();
        FinishVideo();
   }

    public void playvideo() {


        VideoView videoview = (VideoView) findViewById(R.id.videoview);
        Uri uri = Uri.parse("android.resource://" + getPackageName() 
        + "/"+ R.raw.sound_2);
        videoview.setVideoURI(uri);
        videoview.start();
    }

    public void FinishVideo() {
        VideoView videoView = (VideoView) findViewById(R.id.videoview);
             videoView.setOnCompletionListener
    (newMediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer player) {

                //Log.i("VideoView", "onCompletion()");
                //Intent intent = new  Intent
    (PlayVideo.this,MainActivity.class);
                //startActivity(intent);
                System.exit(0);

            }
        });


      }
  }

----------------------------------------------------------------------------

 <RelativeLayout xmlns:android=
  "http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayVideo">


    <VideoView
        android:id="@+id/videoview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在开始第二项活动

时,您必须finish() 第一项活动
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
finish();

在第二个活动中完成视频播放后,您必须致电onBackPressed()

videoview.setVideoURI(uri);
videoview.start();
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        onBackPressed();
    }
});

您不必调用FinishVideo()方法,您可以在setOnCompletionListener方法本身中设置playvideo()