MediaPlayer 在release()后崩溃; .. 不“引起”在LogCat中,我可以调试吗?

发布于 2024-12-23 18:28:08 字数 3667 浏览 2 评论 0原文

基本上,当我单击一个按钮时,它会播放声音,它是一个音板。我遇到的主要问题是当我第一次使用 release(); 播放音频时,当我去再次播放声音只会使应用程序崩溃。我在 OnCompletion 方法中尝试了多种组合,对它们进行了更改,但没有任何效果。无论我输入什么代码,当我输入 release(); 时,它都会崩溃。这是我的代码;

    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>

这是我的 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)

就是这样。有人能猜出问题所在吗?我尝试调试这个问题,弄乱了 IllegalStateException。我不知道怎么办。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-12-30 18:28:08

我认为这些行:

onPause();
onStop();

正在调用您的活动暂停和停止方法。我不知道这是否是你的目标。通过调用这些方法,您将结束您的应用程序(将其从活动堆栈的顶部删除)。您可能会收到错误,因为您在结束后尝试执行某些操作。

如果您的目标是在媒体完成时退出您的活动,那么让您的完成监听器看起来像这样:

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

或者您可以在此处调用 finish() ,然后覆盖您的活动 onStop() 方法并在其中包含 release() 调用。

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.

巡山小妖精 2024-12-30 18:28:08

既然您正在尝试演奏音板,为什么不直接使用 SoundPool 呢?

Since you're trying to play a soundboard, why not just use SoundPool?

泪痕残 2024-12-30 18:28:08

尝试下面的代码:-

 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();
                    }

                });
            }
        });

您必须从侦听器中删除release()。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文