Nuwud
Nuwud

Reputation: 251

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.

Upvotes: 25

Views: 14491

Answers (3)

Hugo Gresse
Hugo Gresse

Reputation: 17899

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.

Upvotes: 4

leegor
leegor

Reputation: 532

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"

Upvotes: 3

Alessandro
Alessandro

Reputation: 136

The native Android Browser doesn't buffer the media files (audio or video), so there is no way to do it on HTML5. As @bradenV2 said, you could copy the file to memory (you could use Phonegap if it is an App).

Upvotes: 0

Related Questions