VideoView stop display when activity resumed and r

2019-08-05 05:09发布

问题:

I'm using VideoView to display a rtsp streaming video. Below you can find a snippet of the code.

OnCreate() {
    ...
    // Setup video view
    videoView = (VideoView) findViewById(R.id.videoViewStream);
    videoView.setMediaController(new MediaController(this));
    ....
    // Create a button
}

OnButtonClickListener() {
    videoView.setVideoURI(Uri.parse(videoUri));
videoView.requestFocus();
}

I have no code concerning the VideoView in the OnResume and OnPause functions.

The use case for the problem is:

  1. Open the application and start the video
  2. I minimize the application to get to the Android Desktop
  3. Use other applications
  4. Re-open or re-trigger the Video Application
  5. Video is not display and not running. I have to reclick on the VideoView

The issue is then, how is it possible to get the video to be redisplayed again? Should I have to put start(), stop() or pause() the video?

Any help is welcome.

TA

回答1:

I would recommend you to save current video postition (and all you need in case of restart) and stop playback in onPause. In onDestroy you need to release mediaPlayer. If user returns you will then get onResume or onCreate. In onCreate you init player again like the first time. In onResume you create a safety check if player is really inited and if everything is ok start playback (or wait for click event). Check also last saved position for possible seek.

I would extract code from OnButtonClickListener into separate method and call that method from clickListener or in onResume if you wish to autostart playback.

Updated answer:

I made a mistake with release. It's done by VideoView in stopPlayback method (look into VideoView source code on grepcode). I don't know how your RTSP streaming server works, but streaming server that I use doesn't support seeking with VideoView's seekTo method. You can call pause in onPause and start in onResume but this will start stream from 0 (in my case). So I had to create custom seekbar. But that's another story.

In my implementation I'm calling pause in onPause and stopPlayback in onDestroy. In onPause I also store last position. In onCreate I only get view and set media controls. In onResume I check if I have any info about last position. If this exist then I prepare player (setVideoPath, requestFocus, start) with that parameter (seek), otherwise without (seek = 0).

That's my solution :)