Search code examples
javaandroidandroid-mediaplayer

Android MediaPlayer `Prepared` and `OnPlayStarted` questions


Two questions:

Without subclassing, is it possible to tell directly from a MediaPlayer instance whether it is in the Prepared state or not -- or even get it's state at all? I've read this and gone through the MediaPlayer docs, and it appears there is no way. I suspect that my only way out is to subclass, but I would like to avoid it if at all possible.

Second, I have read there can be latency between OnPreparedListener and playback actually beginning. Is there a way to tell when a MediaPlayer instance has started playback of a local file? I've tried hooking into the listeners for OnBufferingUpdateListener, OnInfoListener, and OnSeekCompleteListener, but they do not appear to be triggered at all after invoking start() for local files. Aside from polling to see if getCurrentPosition() has changed (admittedly a stupid idea), I can't see any way to see if this is even possible.


Solution

  • To answer your first question: I don't think that MediaPlayer itself maintains a current state, or atleast I have never come across anything as such, even the sample apps I downloaded used a wrapper or subclassing and maintained state of the MediaPlayer. I have done the same in my app.

    To answer your second question you could use MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START in your OnInfoListener callback but only for API 17 and above. According to the docs

    MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START
    

    The player just pushed the very first video frame for rendering.