Stop auto buffer in VideoView android

2019-08-27 16:33发布

问题:

I am implementing Video Downloader App. In which user can view video before downloading it.But problem is that when activity starts video starts auto buffering, which waste more internet data of user. I have provided videoview with download button so user can view video if he want's and then download. Because of auto buffer user needs twice the data for video.

Is there any way to stop auto buffer video as soon as activity/videoview comes in focus. All point of my question is to conserve user data.

Videoview code :

 var videoView = FindViewById<VideoView>(Resource.Id.videoView1);
        var uri = Android.Net.Uri.Parse(url);
        videoView.SetVideoURI(uri);
        videoView.SeekTo(100);
        MediaController mediaController = new
        MediaController(this);
        mediaController.SetAnchorView(videoView);
        videoView.SetMediaController(mediaController);

Btw someone asked this question 3 years ago no one answered it yet. Control buffering in VideoView

Any help be appreciated. TIA !!!

回答1:

VideoView uses the MediaPlayer class internally and doesn't give you any control over starting or stopping the buffering. In fact, buffering of the video starts when PrepareAsync is called in the MediaPlayer class.

Now, when we look at your code, there's a call to SetVideoUri method. Internally that method calls a private openVideo method which prepares the MediaPlayer instance and calls prepareAsync which starts the buffering. If you are really interested in the internal implementation, you can take a look at the C++ source here. The takeaway from all this is that you won't be able to stop buffering taking place if you use the VideoView.

Solution: Use ExoPlayer. ExoPlayer is an open source library that gives you a better control over media buffering and playback. It's endorsed by Google in their documentation and the developer actually appeared in Google I/O event with a session about the project.

ExoPlayer has an interface called LoadControl which is used for controlling the buffering of media. You could try to use the DefaultLoadControl implementation or roll out your own.

Here's a tutorial and a demo app on how to get started with the library.

I know there are other ways to achieve your goal but I doubt you want to risk the project by fully implementing your own buffering mechanism.