我在我的活动中使用MediaPlayer.
当我点击后退按钮,我得到这个错误:
09-20 19:44:16.540: E/MediaPlayer(1822): pause called in state 64 09-20 19:44:16.540: E/MediaPlayer(1822): error (-38,0)
码
public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { if (mp!= null && mp.isPlaying()) { mp.stop(); } Intent intentstart = new Intent(X.this,Y.class); intentstart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intentstart); } return super.onKeyDown(keyCode,event); }
如果我使用mp.pause(),它的工作正常.为什么?
解决方法
暂停停止的MediaPlayer是非法的,并且根据该错误消息听起来完全一样.
我建议改变你的onPause,这样它不会试图暂停停止的MediaPlayer.
也许:
if(mp!= null) { if(mp.isPlaying()) mp.pause(); }
其实不这样做,我刚才在文档中找到了:
Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls to isPlaying(),
您应该在本地维护一个变量,以检查您是否已经停止MediaPlayer,然后测试是否应该调用pause().