25

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.

Nuwud
  • 251
  • 1
  • 3
  • 6

3 Answers3

4

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.

sanjeeb
  • 87
  • 1
  • 12
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
  • Thank you for this answer Hugo. Id like to create a video playlist application. The videos will be over IP. Id like the transition between each video to be instant. Before a video finishes playing, id like to prebuffer the upcoming next video. Should I use two instances of the Exoplayer? Is this possible? Would you recommend this approach? – code Dec 14 '14 at 02:50
  • I'm not sur that exoplayer support many instance but I'm sure that there should be a way to do it. I you consider using ExoPlayer, you can open an Issue on the github : https://github.com/google/ExoPlayer – Hugo Gresse Dec 14 '14 at 11:57
  • how do you preload a video with this player? – user1974368 Aug 26 '18 at 19:04
  • @user1974368 the `player.prepare(...)` as stated in the answer will do. – Hugo Gresse Aug 27 '18 at 15:07
3

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"

leegor
  • 472
  • 5
  • 15
  • thanks for this answer. Id like to create a video playlist that will prebuffer the upcoming video. Should I use 2 MediaPlayers for this? Id like the transition between each video to be instant. I was thinking of alternating between the 2 players. Would you recommend this approach? – code Dec 14 '14 at 02:47
  • You may do that approach or you can create a "Preload Helper" class that pre-download for upcomming video. – leegor Dec 14 '14 at 04:56
  • If video A is playing and I set data source to video B and call prepareAsync(), will that stop running video A in the mediaPlayer? By Preload Helper class, will that use a second mediaPlayer ? Also, what are your thoughts on using the new ExoPlayer android class? Thanks! – code Dec 14 '14 at 05:40
  • To stop Video A by calling `stop()` then set data source to B and call prepareAsync(). Preload Helper class I meant you create a class that pre download upcoming video in background. When ready, set the media player source to the predownloaded file. – leegor Dec 14 '14 at 15:02
0

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).

Alessandro
  • 136
  • 1
  • 9