Search code examples
javaandroidbackgroundandroid-videoview

How to replay a video after going back and how to loop the video in background


First Problem I am making a guessing android game using java and I was wondering how can I use a video as the background of the home screen rather than having an image or simple colors? I got the answer of this question from another question here, but I have this new problem now- The video is playing in background nicely, but whenever I open another activity and go back to the home screen (where the video was playing) the screen showing black screen. Also when the android goes to sleep mode, I start the phone and same problem again!

Second Problem How to loop a video in background, my video is about a minute long, so after a minute the video is getting paused. So, how to loop it?

My code is below-

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        setTitle("");
        videoBackground();
    }

    public void videoBackground(){
        VideoView simpleVideoView = (VideoView) findViewById(R.id.videoView);
        simpleVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bg_video));
        simpleVideoView.start();
    }
public void exitGame(View v){
        finish();
        System.exit(0);
    }

    public void newGame(View v){
        Intent new_game = new Intent(this,ModesActivity.class);
        startActivity(new_game);
    }

    public void howToPlay(View v){

    }

Now I know what's the problem here but I don't know the solution. The thing is someone actually answered this question in stack overflow, I am copying the answer here-

NatureDevil answer and video is great but 2 things are missing first if you click on a button and open a new activity like sing-up and decided to click on back arrow on the device, the home screen will give black screen because the video will not restart so you need to add this

@Override
protected void onResume() {
    super.onResume();
    // to restart the video after coming from other activity like Sing up
    mVideoView.start();
}

So, I didn't understand where to put this specific method. I didn't understand any of it at all. Due to low reputation, I couldn't add a comment there. Can you all help me with this?

Thank you for listening.


Solution

  • You can try this

      public class HomeActivity extends AppCompatActivity {
        VideoView simpleVideoView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            videoBackground();
    
        }
    
        public void videoBackground(){
            simpleVideoView = (VideoView) findViewById(R.id.videoView);
            simpleVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bg_video));
            simpleVideoView.start();
            simpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.setLooping(true); 
                }
            });
        }
        @Override
        protected void onResume() {
            super.onResume();
            simpleVideoView.start();
        }
        @Override
        protected void onPause() {
            super.onPause();
            simpleVideoView.stopPlayback();
        }
    }