Mediaplayer报错
prepareAsync called in state 1
是因为在setDataSource之前调用了prepare。因为setDataSource放到了线程里,忘了start(事实证明也不能放到线程中,因为会和prepareAsync异步执行,导致还没setDataSource,就prepareAsync了)
Error (-38, 0) stop called in state 0
是因为 比如还没播放,用户就点了返回。没start() 没prepare(),就直接stop() 了
start called in state 64
是因为stop()之后直接start()播放了,没有重新prepare()。
容易ANR的:reset() release() setDataSource() prepare()(所以要用prepareAsync,但是要设置prepared监听,准备完才能播放start)
media server died错误,需要实现接口 onErrorListener(on Error())。在what == MediaPlayer.MEDIA_ERROR_SERVER_DIED 时,重新reset,release,置null,然后 new MediaPlayer(), 再从setDataSource开始流程
@Override public boolean onError(MediaPlayer mp, int what, int extra) { if(what == MediaPlayer.MEDIA_ERROR_SERVER_DIED){ Log.d("VideoPlayer", "MEDIA_ERROR_SERVER_DIED"); try{ reInitVideoPlayer(); }catch(Exception e){ }finally{ playVideo(mLoadingImage); } } return false; }
reset() 在重置MediaPlayer的时候使用,重置完,重新setDataResource prepare start,播放下一首。
In order to reuse a MediaPlayer object that is in the Error state and recover from the error, reset()
can be called to restore the object to its Idle state.
也就是说,为了重用处于Error错误状态的MediaPlayer对象,可以通过调用reset()方法,使其恢复到idle空闲状态。
release() 不是每首歌之后都release,而是最后不用MediaPlayer了。比如在onPause或者 onDestroy里边调用。