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

12,724

Solution 1

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.

Solution 2

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"

Share:
12,724

Related videos on Youtube

Nuwud
Author by

Nuwud

I love techy stuff.

Updated on June 04, 2022

Comments

  • Nuwud
    Nuwud almost 2 years

    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.

    • braden
      braden about 12 years
      Do you want to only buffer a portion of the video? If not, you could download the MP4 to flash memory then play like a file.
    • Kevin Parker
      Kevin Parker about 12 years
      Not sure, but did you try calling start then immediately call pause or stop?
    • Jesson Atherton
      Jesson Atherton almost 10 years
      did you ever find a solution?
  • code
    code over 9 years
    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
    code over 9 years
    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?
  • leegor
    leegor over 9 years
    You may do that approach or you can create a "Preload Helper" class that pre-download for upcomming video.
  • code
    code over 9 years
    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!
  • Hugo Gresse
    Hugo Gresse over 9 years
    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 : github.com/google/ExoPlayer
  • leegor
    leegor over 9 years
    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.
  • user1974368
    user1974368 almost 6 years
    how do you preload a video with this player?
  • Hugo Gresse
    Hugo Gresse almost 6 years
    @user1974368 the player.prepare(...) as stated in the answer will do.