3

I have been working on playing videos using VideoView in Android.

OnPause of fragment i am pausing the video and storing the seektime

    @Override
    public void onPause() {
            super.onPause();

        if (videoview != null && videoview.isPlaying()) {
            videoview.pause();
            seekTime = videoview.getCurrentPosition();

        }
    }

OnResume i am trying to resume the video using -

        @Override
        public void onResume() {
            super.onResume();
            videoview.seekTo(seekTime);
            //vvOnboardingVideos.resume();// this is not working for now - need to investigate
            videoview.start();   
        }

The video plays from the beginning.

Is there anything i am doing wrong. Please suggest.

Anukool
  • 5,301
  • 8
  • 29
  • 41

2 Answers2

1

Try to reverse order of operations :

@Override
public void onResume() {
    super.onResume();
    videoview.start();
    videoview.seekTo(seekTime);   
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31
0

try this:

save the video position when onpause() called

// This gets called before onPause
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        stopPosition = videoView.getCurrentPosition(); 
        videoView.pause();
        outState.putInt("video_position", stopPosition);
    } 

Then in your onCreate() load the stopPosition from the bundle

@Override
    protected void onCreate(Bundle args) {
        super.onCreate(args);
        if( args != null ) {
            stopPosition = args.getInt("video_position");
        }
    }

Resume the video using:

@Override
    public void onResume() {
        super.onResume();
        Log.d("Tag", "onResume");
        videoView.seekTo(stopPosition);
        videoView.start(); //Or use videoView.resume() if it doesn't work.
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62