tyc*_*czj 7 android android-mediaplayer
我有一个AlertDialog,当我点击时停止播放声音,但在某些设备上看起来调用onStop()抛出了IllegalStateException,但为什么?
如果对话框已启动,则表示声音正在播放,因此应该是音频未播放的情况.
我暂时把它包围起来,但这会导致什么呢?
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try{
mp.stop(); //error
mp.reset();
mp.release();
}catch(Exception e){
Log.d("Nitif Activity", e.toString());
}
v.cancel();
popupMessage();
finish();
}
});
Run Code Online (Sandbox Code Playgroud)
zap*_*apl 22
检查mp != null可以防止NullPointerException但IllegalStateException不能由此引起.
你得到这个错误的原因是玩家处于不能的状态stop().如果您查看MediaPlayer文档顶部的状态图,您可以看到只有在播放器处于Prepared状态后才能调用stop .下一种可能性是您已经调用release()或者reset()也会导致该错误.
你可以调用stop()只能在Prepared,Started,Paused,PlaybackComplete或Stopped状态.所有其他州产生该错误.
因此,您可以prepareAsync()在玩家准备好之前按下按钮,或者在按下按钮之前有代码释放或重置播放器.
我猜您可能在执行这些行之前将实例清空。当我收到此错误时,我首先检查是否为空。
if (mp != null) {
try {
mp.stop(); //error
mp.reset();
mp.release();
} catch(Exception e){
Log.d("Nitif Activity", e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)