質問

Trying to put a "pause" button into my application that plays a few sound clips looping.

When I call mp.pause(); all hell breaks loose, I'm totally lost!

Here is the method I'm using..

    protected void managerOfSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();
    }

        if (theText.equals(campfire))
            mp = MediaPlayer.create(this, R.raw.campfire);
        else if (theText.equals(calmthunder))
            mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
        else if (theText.equals(rainthunder))
            mp = MediaPlayer.create(this, R.raw.rainthunder);
        else if (theText.equals(whalesgulls))
            mp = MediaPlayer.create(this, R.raw.whalesandgulls);
        else if (theText.equals(stopplaying))
            mp.pause();

    mp.start();
    mp.setLooping(true);
}

And here's a logcat (Meow ^_^)

threadid=1: thread exiting with uncaught exception (group=0xb2f6c648)
FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaPlayer.setLooping(Native Method)
at com.tags4apps.soothingsounds.MainActivity.managerOfSound(MainActivity.java:83)
at com.tags4apps.soothingsounds.MainActivity$1.onClick(MainActivity.java:34)
android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

I'm open to any suggestions right now

edit: What's with the downvotes, I'm 7pm I've been looking for a way to resolve this since 3pm. So downvoting it saying that It's lack of research is kind of unfair..

役に立ちましたか?

解決

This may not be a complete answer, but I'll attempt to address a fairly obvious problem in the current code:

protected void managerOfSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();   //from this point on it is illegal 
                        //to operate on the existing mp object
    }
//if you take one of these branches you will get a new mp instance, and be fine
    if (theText.equals(campfire))
        mp = MediaPlayer.create(this, R.raw.campfire); 
   else if (theText.equals(calmthunder))
        mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
    else if (theText.equals(rainthunder))
        mp = MediaPlayer.create(this, R.raw.rainthunder);
    else if (theText.equals(whalesgulls))
        mp = MediaPlayer.create(this, R.raw.whalesandgulls);
//but if you take this branch, you will be illegally operating on a released instance
    else if (theText.equals(stopplaying))
        mp.pause();

    mp.start();
    mp.setLooping(true);
}

Instead, do something like this:

protected void managerOfSound(String theText) {
    if (theText.equals(stopplaying)) {
        if (mp != null) mp.pause();

    } else {  //we are not pausing
        if (mp != null) {  //lets release any old one
            mp.reset();
            mp.release();   
        }
        //and then get an appropriate new instance
        if (theText.equals(campfire))
            mp = MediaPlayer.create(this, R.raw.campfire); 
        else if (theText.equals(calmthunder))
             mp = MediaPlayer.create(this, R.raw.calmthunderstorm);
        else if (theText.equals(rainthunder))
             mp = MediaPlayer.create(this, R.raw.rainthunder);
        else if (theText.equals(whalesgulls))
             mp = MediaPlayer.create(this, R.raw.whalesandgulls);
        //Note this will still fail if there is some OTHER possibility
        mp.start();
        mp.setLooping(true);
      } 
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top