I have an app that plays randomly every second 1 of 20 different sounds . After almost 1000 successful times the media player create function starts to return always null. The problem remains even when I leave the app and I start it again. The only solution is when I install the app again or I switch off and on the device.
Is there any method to recover from this state? If I do release or reset, media player was already null and they produce an exception.
The sequence I do every second is the following:
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?
}
It seems I have found the solution. I have now played more than 10000 audios without reproducing the error anymore.
I want to thank to kcoppock for his help, I do not create and release now because it is much better to change the data source as he explained, but it was not the main problem.
The final solution was to convert all the mp3 files into ogg files !!!!
Media Player has definitely problems with mp3 files.