Question

I'm using MediaPlayer on Honeycomb and cannot get the duration of any HLS (http live streaming) video in the function onPrepared().

If getDuration() is called after prepare(), it returns 0:

public void onPrepared(MediaPlayer mp) {
    // getduration returns 0
    mediaPlayer.start();
    Log.d(TAG, "duration: " + mediaPlayer.getDuration());
}

However, the videos starts playing.

If getDuration() is called in onVideoSizeChanged() it returns the correct value,

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    // returns the correct value
    Log.d(TAG, "duration: " + mediaPlayer.getDuration());
}

Why does getDuration() work in onVideoSizeChanged() but not in onPrepared()?

Edit: At first I thought getDuration() doesn't work at all, but found it to return the correct value in onVideoSizeChanged(). Modified question to reflect this.

Was it helpful?

Solution

There isn't anything you can do about this. The stream details won't be known until the stream is being read. Depending on the format there may be correct information near the front of the stream, a hint or nothing.

Since the video size change listener is always called you'll have to restructure your code to get the duration then (if it is available).

(Even if this is a bug and even if it is fixed, such a fix would not be deployed to millions of existing devices so you are stuck with current behaviour.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top