I am building an app that is successfully displaying an MP4 video file onButtonClick. I want to pre-buffer or preload the video's URI (remote url) so that it doesn't delay the playing of the video once the button is clicked. I want it to click and play right away, so pre-loading or buffering on the app launch splash screen seems like a fitting solution. Only thing is I don't know how. I have tons of Android Books, but hardly any of them cover buffering at all or they only cover audio.
Can anyone let me know how to buffer the video on a previous activity?
Thanks.
Google released ExoPlayer which provides a higher level for media playing : http://developer.android.com/guide/topics/media/exoplayer.html
It supports different state such as buffering in background :
// 1. Instantiate the player.
player = ExoPlayer.Factory.newInstance(RENDERER_COUNT);
// 2. Construct renderers.
MediaCodecVideoTrackRenderer videoRenderer = …
MediaCodecAudioTrackRenderer audioRenderer = ...
// 3. Inject the renderers through prepare.
player.prepare(videoRenderer, audioRenderer);
I used it in my own project and it seems pretty efficient. Also Google made a Full Demo of the player : https://github.com/google/ExoPlayer/tree/master/demo/src/main/java/com/google/android/exoplayer/demo/full which is more powerfull than simple demo.
You can use the MediaPlayer for preparing the video, like this:
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(<implementation of MediaPlayer.OnPreparedListener>);
mediaPlayer.setDataSource(dataSource);
mediaPlayer.prepareAsync();
After the call prepareAsync()
the mediaplayer will buffer your video.
The MediaPlayer.OnPreparedListener.onPrepared()
will tell you if the mediaplayer is ready to play yet.
Just check the prepared flag and call mediaplayer.start()
when you click your button "onButtonClick"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With