Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-Load or Pre-Buffer .mp4 video in android app development

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.

like image 584
Nuwud Avatar asked Aug 28 '11 19:08

Nuwud


2 Answers

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.

like image 139
Hugo Gresse Avatar answered Nov 14 '22 11:11

Hugo Gresse


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"

like image 31
leegor Avatar answered Nov 14 '22 09:11

leegor