I am using the following code to play a sound and after a while it will stop playing sounds, this is because of too many instances of mediaplayer open I believe so I added an extra mp.release();
and it just crashes my app (it's currently commented out).
Here is the actual code I am using.
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
if (mp!=null){
// mp.release();
}
try {
mp.setDataSource(path+"/"+fileName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
and to call it in my app I use:
audioPlayer(sdcard + "/soundboard","s1sound1.ogg");
can anyone tell me why it's crashing and what do I need to do to fix it?
From the documentation,
after release the object is no longer available
So you can't perform any action on it. if you create a new MediaPlayer
object, there's no need to call release on it anyway.
Instead, you can do:
public void audioPlayer(String path, String fileName){
if (mp != null)
mp.release();
mp = new MediaPlayer();
....
And declare MediaPlayer mp;
as a class member, not local.