Android MediaPlayer takes long time to prepare and buffer

25,524

Solution 1

I have struggled with this question for months. Finally i found the solution.

The real problem is in the implementation of the MediaPlayer class. Particularly with the way MediaPlayer buffers the data. This is why the solution is to create your own buffering, save it to a temp file and feed that to MediaPlayer.

This tutorial and source code explain exactly how. http://androidstreamingtut.blogspot.nl/2012/08/custom-progressive-audio-streaming-with.html

By adapting this code, it is easy to create a much better streaming player.

Google Developers really screwed up here.

EDIT : This answer is rather old. Nowdays i would recommend not using MediaPlayer and use ExoPlayer instead. It is extendable, stable and can play many different types of media. You can find it here: https://github.com/google/ExoPlayer/

Solution 2

There really isn't much you can do since the Android MediaPlayer class doesn't provide access to lower level settings such as buffer size. The only alternative would be to make your own player using AudioTrack and a library like FFmpeg to do the decoding.

Share:
25,524

Related videos on Youtube

SteveEdson
Author by

SteveEdson

Newcastle University Information Systems w/ Business Graduate. Learning Android Development. Web Developer at Jaywing, Sheffield SOreadytohelp

Updated on October 07, 2020

Comments

  • SteveEdson
    SteveEdson over 3 years

    My application takes a long time to prepare and buffer an audio stream. I have read this question Why does it take so long for Android's MediaPlayer to prepare some live streams for playback?, however it just says people have experienced this issue, it does not state how to improve the problem.

    I am experiencing this in all versions of Android, tested from 2.2 - 4.1.2.

    The streams are in a suitable bit-rate for mobile and 3G connection. The same stream takes less than a second to start buffering in the equivalent iOS app.

    Is there a way to specify the amount of time that should be buffered? I know that the Tune In radio application offers this feature ( https://play.google.com/store/apps/details?id=tunein.player ).

    Thanks.

    Edit: I've tested again and found that it only happens on devices running Gingerbread and above (>=2.3). I know that Android changed the underlying framework from OpenCore to StageFright. So how can I optimise the media framework? It just seems wrong that the old HTC Wildfire can prepare, stream and play, literally 10x faster than the brand new HTC One X and Nexus 7.

  • SteveEdson
    SteveEdson over 11 years
    Thanks for this suggestion, unfortunately in this case, it is not a suitable option, as the streams are provided and are out of my control. This might work if I had control of the streams.
  • SteveEdson
    SteveEdson over 11 years
    Thanks, since asking this question I've come to the conclusion that I need to use the FFMpeg library. I've not heard of AudioTrack however, so I will look into this. I'm struggling figuring out how to implement FFMpeg within my application, but I guess that is for another question.
  • William Seemann
    William Seemann over 11 years
    Steve, I developed an open source Android application that likely does what you are trying to accomplish. I also just added FFmpeg support so I can stream protocols like mms://. See the code here: sourceforge.net/projects/servestream . If you have any questions you can always contact me via SourceForge. I hope this helps.
  • ajacian81
    ajacian81 over 11 years
    The one thing to note is that if you use FFMPEG you may need to open source your project (depending on which codecs you use either GPL or LGPL license). Read here for more info: ffmpeg.org/legal.html
  • Juan Carlos Ospina Gonzalez
    Juan Carlos Ospina Gonzalez about 10 years
    Note: this solution is not suitable for all scenarios as sometimes the MediaPlayer will 'stutter' while pausing, loading the new buffer and restarting. MediaPlayer is just a poorly written class.
  • SteveEdson
    SteveEdson about 10 years
    Thanks, this sounds like it could be a suitable alternative, however, I've started using FFMpeg in my app now.
  • Juan Carlos Ospina Gonzalez
    Juan Carlos Ospina Gonzalez about 10 years
    Yeah, i'm trying to get that done following the NDK cookbook as a guide. If you have any good tutorials that actually got you setup do link to them. I can't believe the Android platform is so inadequate for such an apparently trivial task that you have to go to great lengths like porting code from a different platform.
  • burakk
    burakk about 10 years
    Hi William, I tried your media player on my Note 3 (Android version 4.3) and still getting the same behavior, playing the music for about one second, then waiting up to 7*8 seconds and resuming... This is the same behavior as the normal MediaPlayer implementation. How can I fix it, by modifying the buffer size? in DownloadTask class (DownloadPlayer.java)
  • burakk
    burakk about 10 years
    Hi SteveEdson, are you using the code from the ServeStream by William Seemann? Why did you not go for a stream proxy solution?
  • SteveEdson
    SteveEdson about 10 years
    Yes I am. My application needs to be able to play several different codecs and formats, as well as being able to stream efficiently, so Williams example seemed like a sensible approach.
  • Jameson
    Jameson over 8 years
    I get the exact same thing as burakk. The more I use Android, the more I am disappointed.