KC Chai
KC Chai

Reputation: 1617

Videoview Restarts after pressing home/power button on phone

Below are my codes. My videoview restarts everytime you press on the home/power button. Are there any way i can make it continue where my video has been playing before pressing the home/power button.

Edited : I have put in the codes mentioned by Bharat. But it seems that I'me getting error vd not resolved.

 public class Video1 extends Activity {
    private MediaController mc;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videomain);

    //AdView adView = (AdView)this.findViewById(R.id.adView);
    //adView.loadAd(new AdRequest());

    VideoView vd = (VideoView) findViewById(R.id.VideoView);

    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.opening);

    mc = new MediaController(this);
    vd.setMediaController(mc);

    vd.setVideoURI(uri);
    vd.start();



    vd.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
    {           
        public void onCompletion(MediaPlayer mp) 
        {
            Intent intent=new Intent(Video1.this,GalleryVideo.class);
            startActivity(intent);

        }           
    });   
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
@Override
protected void onPause() {
    vd.pause(); /**ERROR - vd cannot be resolved**/
    super.onPause();
}


@Override
protected void onResume() {
    vd.resume(); /**ERROR - vd cannot be resolved**/
    super.onResume();
}
    }

Upvotes: 0

Views: 1945

Answers (1)

Bharat Sharma
Bharat Sharma

Reputation: 3966

Try this in your class

@Override
protected void onPause() {
    vd.pause();
    super.onPause();
}


@Override
protected void onResume() {
    vd.resume();
    super.onResume();
}

when your activity will go on pause it will pause your video view. when your activity will resume it will resume it in on resume.

Upvotes: 1

Related Questions