49

I am new to android development and I am programming a game. My game has cutsceens that play before each level starts, cutsceens which are done through videoview. My problem is, that upon an application pause, the cutsceen starts from the beginning again when resumed.

    @Override
public void onPause() {
    super.onPause();
    video.pause();
}
@Override
public void onResume()
{
    super.onResume();
    video.resume();
}

Our issue is that the video doesn't actually resume from where we paused it, but from the beginning.

Athos
  • 681
  • 2
  • 7
  • 14

7 Answers7

67

You can use this

@Override
public void onPause() {
    Log.d(TAG, "onPause called");
    super.onPause();
    stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
    videoView.pause();
}
@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume called");
    videoView.seekTo(stopPosition);
    videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}

original post

Community
  • 1
  • 1
Sanket Patel
  • 1,130
  • 14
  • 25
  • 4
    Doesn't work for me. It only starts from the beginning anyway... Android 4.4.4 – JohnyTex Apr 27 '16 at 14:45
  • 2
    Btw above solution works in simplest flow. @JohnyTex https://examples.javacodegeeks.com/android/android-videoview-example/ nice example with code, may helps you. – Sanket Patel Apr 28 '16 at 10:25
  • This didn't work for me on a Samsung Galaxy S5 with 5.0. The solution below by @Khang .NT , however, works perfectly fine. – Dean Panayotov Jul 21 '16 at 20:35
  • @DeanPanayotov Dear, answer was at "answered Oct 8 '13 at 6:49". there are lots of updates in android api since that time. – Sanket Patel Jul 22 '16 at 09:48
  • 2
    @SanketPatel just clarifying that the currently selected correct answer is probably not suitable for any modern versions of Android. I don't really care when this question got an answer. It was indexed on the top of the first page in Google so I'm simply trying to make it a bit more relevant. Please let me know if there are any guidelines on SO regarding old questions/answers. Thanks! – Dean Panayotov Jul 22 '16 at 13:34
  • It doesn't seem precise to me, when i take this approach. It seems like it jumps a little back in the video, when i start it after being paused. But it helped me. Summed up 10 seconds mVideoView.seekTo(stopPosition+(10*1000)); – Dyno Cris Sep 29 '22 at 19:05
30

Shouldn't use video.pause(), video.resume(), because when you call it, the buffering data will be lost. That is also the reason WHY VideoView play at BEGINNING whenever you call video.resume(). See it: VideoView onResume loses buffered portion of the video

Solution:

VideoView videoView;
MediaPlayer mp;

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                this.mp = mp;
            }
        });

public void pause(){
    //NOT videoview.pause(); Needn't save Stop position
    if (mp != null){
       mp.pause();
    }
}

public void resume(){
    //NOT videoview.resume();
    if (mp != null){
       mp.start(); //Video will begin where it stopped
    }   
}
Community
  • 1
  • 1
Khang .NT
  • 1,504
  • 6
  • 24
  • 46
  • Can you please explain where should we use these `pause()` and `resume()` methods? – Akeshwar Jha Mar 14 '16 at 09:28
  • I don't known, I have read VideoView class, it's very simple, just implement media player with SurfaceView, i don't known why VideoView clear buffering data whenever we Pause(). – Khang .NT Mar 14 '16 at 15:30
  • Worked well on Galaxy S5 with 5.0. – Dean Panayotov Jul 21 '16 at 20:35
  • 10
    I'm getting IllegalStateException when `mp.start()` is called – Lee Kang Aug 22 '18 at 23:36
  • Though the code did not work, I found this answer useful because of the info and provided link. – Chisko Jan 28 '19 at 20:51
  • Oh god, bless you, this worked fine. setOnPrepared section should be in onCreate, then another public void comes after onCreate in my case. My video pauses, then continues. Thank you so much! – Bay Feb 03 '23 at 22:53
20

Instead of resume(), use start() again.
It will start the playback from the point where you paused the video.

ridoy
  • 6,274
  • 2
  • 29
  • 60
Flynn81
  • 4,108
  • 29
  • 28
  • 15
    No it's not. It starts from the beginng of a video. – I4004 Jan 10 '13 at 18:21
  • That doesn't work for me in 5.1.1, it does go back to the beginning though. – timeartist Jun 11 '15 at 02:25
  • 3
    It's messed up, but start() after pause() works and resume() does not on Nexus Player (5.1.1). – Dustin Jun 17 '15 at 22:16
  • it works for me, but as per other people it is not working, very weird they are not even providing any documentation to the method in GoogleDevelopers https://developer.android.com/reference/android/widget/VideoView.html#start() , so, they are leaving it to be tried by luck :( – Muhammed Refaat Nov 16 '17 at 08:18
13

In OnPause you can get the current position .

length=mVideoView.getCurrentPosition();

and length value use in OnResume ().

mVideoView.seekTo(length);
mVideoView.start()

working fine .

1
    int stopPosition;   // Globally declare class level...
    @Override
    public void onPause() {
        super.onPause();  
        onPauseVideoView();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("WatchVideoAd Fragment", "onPause called");

        onResumeVideoView();
    }

    private void onPauseVideoView() {
        stopPosition = videoview.getCurrentPosition(); //stopPosition is an int
        videoview.pause();
    }

    private void onResumeVideoView() {
        videoview.seekTo(stopPosition);
        videoview.start(); //Or use resume() if it doesn't work. I'm not sure
    }

onHiddenChanged in case of Fragment

  @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (getActivity() != null) {
                ((AppActivity) getActivity()).updateTitle(getResources().getString(R.string.title_game));
            }
            Log.e("keshav", "WatchVideoAd Fragment  ");
            onResumeVideoView();

        } else {

            Log.e("keshav", "WatchVideoAd Fragment else ");
            onPauseVideoView();
        }
    }
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

Please try below code it will work absolutely fine

 @Override
public void onPause() {
    super.onPause();
    stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
    if (videoView.isPlaying())
        videoView.pause();
}

@Override
public void onResume() {
    super.onResume();
    if (videoView != null) {
        videoView.seekTo(stopPosition);
    }
}

please make sure below line is mentioned in your Manifest.xml

<activity
        android:name="<Video Play Activity Name Here>"
        android:configChanges="orientation|keyboardHidden|screenSize" 
        android:screenOrientation="landscape"
        ></activity>
Anuj J Pandey
  • 656
  • 1
  • 4
  • 17
  • You are not calling start() again in onResume(). And I'm quite sure that by using seek() the video is seeking ahead of the actual stop position to find the next I-frame thus skipping some seconds of the video (depending on your I-frame rate). – marsbear Jul 26 '17 at 16:07
0

Just use onSaveInstanseState method instead of onPause and use onRestart instead of onResume. It will definitely work. Cheers !