开发者

mp.release crashing soundboard app

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);
    }开发者_如何学C 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜