6

我试图在 onDestroy() 中停止并释放 MediaPlayer 对象,但应用程序不断崩溃。这是代码:

void StartSound(final String ReaderPath) {
    mediaplayer = MediaPlayer.create(test2.this, Uri.parse(ReaderPath));

    try {
        mediaplayer.start();
        mediaplayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                current++;
                try {
                    mp.reset();
                    mp.setDataSource(ReaderPath);
                    mp.prepare();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                mp.start();

            }
        });

    } catch (Exception e) {

    }
}

@Override
    protected void onPause() {
        if (mediaplayer != null && mediaplayer.isPlaying())
            mediaplayer.pause();
        super.onPause();

    }

    @Override
    protected void onStop() {
        if (mediaplayer != null && mediaplayer.isPlaying()) {
            mediaplayer.release();
            mediaplayer.stop();
        }
        super.onDestroy();

    }

日志猫:

08-21 22:52:26.512: E/AndroidRuntime(16410): java.lang.RuntimeException: Unable to destroy activity {com.example.test/com.example.test.test2}: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3235)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3253)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.access$1200(ActivityThread.java:132)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1190)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.os.Looper.loop(Looper.java:137)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at java.lang.reflect.Method.invoke(Method.java:511)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at dalvik.system.NativeStart.main(Native Method)
08-21 22:52:26.512: E/AndroidRuntime(16410): Caused by: java.lang.NullPointerException
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1406)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1876)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:316)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.Activity.performDestroy(Activity.java:4629)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1079)
08-21 22:52:26.512: E/AndroidRuntime(16410):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3222)
08-21 22:52:26.512: E/AndroidRuntime(16410):    ... 11 more

为什么是这样?

4

2 回答 2

4

你为什么从你的 onStop() 方法调用 super.onDestroy() ?你应该调用 super.onStop()。或者这是一个打字错误?

答案可能是您在stop()调用MediaPlayer 调用release()。一旦你调用release()MediaPlayer它就会失效并且不再可用。

于 2012-08-21T20:09:49.640 回答
1

停止和释放MediaPlayer内部onDestroy()方法的控制的正确方法是:

@Override
protected void onDestroy() {
    if (mediaplayer != null) {
        mediaplayer.stop();
         mediaplayer = null;
    }
    super.onDestroy();

}
于 2016-04-06T06:31:37.747 回答