1

For the last 18 Months I have been building simple apps in android studio and I am really stuck trying to work this out so any help would be great

So far my app works great, open the app on the first activity and press the play button and the 2nd activity opens and plays the video, at the end of the video it returns back to the first activity, but I am trying to exit the app

guessing I should be able to add something like

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>
Ketan P
  • 4,259
  • 3
  • 30
  • 36
J.Edwardes
  • 11
  • 3

2 Answers2

1

try this, it works for me, and you do not need to add finish(); function it will close the app if the FirstActivity is the MainActivity

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);

and in Second Activity add this

videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                onBackPressed();
            }
        });
Sarmad
  • 31
  • 4
0

You have to finish() the first activity while starting the second activity,

startActivity(new Intent(FirstActivity.this, SecondActivity.class));
finish();

and when the video play is completed in the second activity you have to call onBackPressed()

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

and you don't have to call FinishVideo() method, you can set setOnCompletionListener in playvideo() method itself

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64