Android MediaPlayer: getDuration() returns 0 after prepare()

时间秒杀一切 提交于 2019-12-12 10:22:55

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/8362879/android-mediaplayer-getduration-returns-0-after-prepare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!