VideoView onResume loses buffered portion of the v

2019-01-08 11:22发布

I am having an Activity in which there is

  1. VideoView -- Streams a video from a webserver.

  2. Button -- Takes the user to the next activity to be shown.

When the application starts, VideoView is made to play the Video from a webserver.

Now assume

 Total Video length is 60 Minutes

 Current Video progress is 20 Minutes

 Current Buffered progress 30 Minutes 

Now when I click on the above mentioned Button which takes user to the next activity.

From that Activity if i press the back button, Previous Activity(with VideoView and Button) appears in front of the user. But when resumed all the Buffered Portion of the video is lost and hence the VideoView starts playing the video from the beginning which is really bad. <-- Actual Problem

Problem

When Activity is resumed back, the buffered portion of the video is lost and hence starts buffering it again. So how to overcome re-buffering the buffered portion of the Video ?

Even official Youtube android app. has the same problem.

Edit 1 :

I tried the below code in Activity but its not working.

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    videoView.suspend();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    videoView.resume();
}

Can anyone guide me regarding this problem ?. Or am I missing something to make this work perfectly ?

Current Workaround

I have saved the current playing position of the video in onPause() method and in onResume() method I have used that position to seek the video to that duration. This works fine. But the video buffering starts from the beginning tho it starts the video from the seek position.

Any help is deeply appreciated.

10条回答
Lonely孤独者°
2楼-- · 2019-01-08 12:01

I found a solution fix it:

VideoView videoView;
MediaPlayer mp;

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

public void pause(){
    //NOT videoview.pause();
    if (mp != null){
       mp.pause();
    }
}

public void resume(){
    //NOT videoview.resume();
    if (mp != null){
       mp.start();
    }   
}

It work for me, i sure it help you

查看更多
甜甜的少女心
3楼-- · 2019-01-08 12:02

The problem with videoView.resume() in onResume() can be seen here: VideoView.resume(). VideoView.resume() calls openVideo() which first releases any previous MediaPlayer instances and then starts a new one. I cannot see an easy way out of this.

I see two possibilities:

  • Write your own VideoView which retains the MediaPlayer instance for as long as you want. Or just take the source and modify to your liking, it's open source (check the license, though).
  • Create a network proxy in your app which stands between the VideoView and the web server. You point your proxy to the web server and the VideoView to the proxy. The proxy starts downloading the data, saves it continuously for later use and passes it to the listening MediaPlayer (which was started by the VideoView). When the MediaPlayer disconnects, you keep the already downloaded data, so that when the MediaPlayer restarts the playback, you don't have to download it again.

Have fun! :)

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-08 12:03
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    videoView.pause();
    super.onPause();
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    videoView.resume();
    super.onPause();
}

try by adding above twomethods in your activity.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-08 12:06

I've worked out a version that does not require a custom VideoView or manual configuration change handling. See Android VideoView orientation change with buffered video for an explanation.

查看更多
爷、活的狠高调
6楼-- · 2019-01-08 12:06

You've mentioned two separate problems, and while I don't know how to keep the buffered video, You can still avoid starting from the beginning by calling getCurrentPosition in onPause and seekTo on onResume. This call is asynchronous, but it might give you a partial solution.

查看更多
Root(大扎)
7楼-- · 2019-01-08 12:08

In onPause() function, instead of

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    videoView.suspend();
}

try

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    videoView.pause();
}
查看更多
登录 后发表回答