我有一个应用程序,每秒随机播放 20 种不同声音中的 1 种。近 1000 次成功后,媒体播放器创建函数开始始终返回 null。即使我离开应用程序并重新启动它,问题仍然存在。唯一的解决方案是当我再次安装应用程序或关闭并打开设备时。
有什么方法可以从这种状态中恢复吗?如果我确实释放或重置,媒体播放器已经为空,它们会产生异常。
我每秒执行的顺序如下:
if (mp != null)
{
if (mp.isPlaying())
{
mp.stop();
}
if (mp != null) mp.release();
if (mp != null) mp = null;
}
mp = MediaPlayer.create(this, R.raw.sound);
if (mp !=null)
{
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
if (mp != null) mp.start();
}
};
}
else
{
// error, what should I do here to recover from this situation?
}