Code Bye

Android MediaPlayer的prepare()方法太耗时问题

       如题,本人用MediaPlayer播放网网络上的mpg视频,先执行mediaPlayer.prepare()方法再start(),但播放有的视频,这个prepare()方法会执行几十秒后才会执行start()方法,或prepare()方法会一直执行不往下执行了,怎么样避免这种情况?
希望各位英雄好汉路过能够提出宝贵的建议,谢谢!
解决方案

60

用异步的prepare()方法,用回调prepareListener回调函数,当回调还没有执行时,应该提示Loading ,提高用户体验。

40

确实是用birdsaction 所说的异步方法prepareAsync ,而prepare是个同步方法,对于播放本地多媒体文件比较适合,官方文档也是这么说的:
public void  prepareAsync ()
Prepares the player for playback, asynchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android MediaPlayer的prepare()方法太耗时问题