简体   繁体   中英

MediaPlayer crashes after release(); .. No 'Caused by' in LogCat, can I debug?

Basically, when I click a button, it plays a sound, it's a soundboard.. The main problem I have is when I use release(); the first time round the audio is played, when I go to play the sound again it just crashes the app. I tried numerous combinations in the OnCompletion method, changing them around, nothing. No matter what code I put, when I put release(); it just crashes. Here's my code;

    public class MyClass extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

        final MediaPlayer mpB1 = MediaPlayer.create(this, R.raw.s101);
        final MediaPlayer mpB2 = MediaPlayer.create(this, R.raw.s102);
            <etc>


        Button b1 = (Button) findViewById(R.id.sound101);
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpB1.start();
                mpB1.setOnCompletionListener(new OnCompletionListener() {

                    public void onCompletion(MediaPlayer mpB1) {
                        // TODO Auto-generated method stub
                        onPause();
                        onStop();
                        mpB1.release();

                    }

                });
            }
        });

        Button b2 = (Button) findViewById(R.id.sound102);
        b2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpB2.start();
                mpB2.setOnCompletionListener(new OnCompletionListener() {

                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        onPause();
                        onStop();
                        mpB2.release();
                    }

                });
            }
        });
                    <etc, you get the idea.. soundboard>

Here is my LogCat;

    01-01 02:58:53.218: E/AndroidRuntime(682): FATAL EXCEPTION: main
    01-01 02:58:53.218: E/AndroidRuntime(682): java.lang.IllegalStateException
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.media.MediaPlayer._start(Native Method)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.media.MediaPlayer.start(MediaPlayer.java:819)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at 'com.package'$1.onClick('Class'.java:73)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.view.View.performClick(View.java:2485)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.view.View$PerformClick.run(View.java:9080)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.os.Handler.handleCallback(Handler.java:587)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.os.Looper.loop(Looper.java:123)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at java.lang.reflect.Method.invoke(Method.java:507)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    01-01 02:58:53.218: E/AndroidRuntime(682):  at dalvik.system.NativeStart.main(Native Method)

That's it. Can anyone guess the problem? I tried to debug the issue, messing with the IllegalStateException. I don't know how.

I think these lines:

onPause();
onStop();

are calling your activity pause and stop methods. I don't know if this is your goal or not though. By calling these methods you are ending your application(removing it from the top of the stack of activities). You are probably getting the error because you are trying to do something after you've already ended.

If your goal is to exit your activity when the media is complete then make your completion listener look like this:

                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mpB2.release();
                    YourActivity.this.finish();
                }

alternatively you could just call finish() here and then override your activities onStop() method and include the release() call inside there.

try below code:-

 b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpB1.start();
                mpB1.setOnCompletionListener(new OnCompletionListener() {

                    public void onCompletion(MediaPlayer mpB1) {
                        // TODO Auto-generated method stub
                        onPause();
                        onStop();
                    }

                });
            }
        });

you have to remove release() from listener.

既然您要播放音板,为什么不只使用SoundPool?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM