发布媒体播放器onDestroy android

时间:2015-09-15 12:24:16

标签: android

我试图在用户打开另一个活动时停止mp3但是,使用的代码似乎并没有停止mp3

public class Tut2 extends Activity {
    private MediaPlayer mp1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tut2);

        final Button button = (Button) findViewById(R.id.filmTransparent22);
        final Handler handler = new Handler();
        final Runnable changeBackground = new Runnable() {
            private int i;

            @Override
            public void run() {
                // Set background based on task execution counter
                if (++i % 2 == 0) {
                    button.setBackgroundResource(R.drawable.btn_tp_light);
                } else {
                    button.setBackgroundResource(R.drawable.btn_tp_dark);
                }

                // Repeat task
                handler.postDelayed(this, 300);
            }
        };

        // Initiate the task
        handler.postDelayed(changeBackground, 300);
        // play sound
        final MediaPlayer mp1 = MediaPlayer.create(getBaseContext(),
                R.raw.plan_one); // -<
        mp1.start();
        // play sound

    }

    public void next2(View view) {
        Intent intent = new Intent(this, Tut3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        // finish(); calling finish makes it not work

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mp1 != null) {
            mp1.stop();
            mp1.release();
            mp1 = null;
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
    }
}

我已经尝试了这个,但它给了我一个例外

 public void next2(View view) {
    mp1.pause();
    mp1.stop();
    mp1.release();
    Intent intent = new Intent(this, Tut3.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(intent);

}

09-15 18:05:04.239: E/AndroidRuntime(30465): FATAL EXCEPTION: main
09-15 18:05:04.239: E/AndroidRuntime(30465): java.lang.IllegalStateException: Could not execute method of the activity
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.view.View$1.onClick(View.java:3838)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.view.View.performClick(View.java:4475)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.view.View$PerformClick.run(View.java:18784)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.os.Handler.handleCallback(Handler.java:730)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.os.Looper.loop(Looper.java:137)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.app.ActivityThread.main(ActivityThread.java:5450)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at java.lang.reflect.Method.invoke(Method.java:525)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at dalvik.system.NativeStart.main(Native Method)
09-15 18:05:04.239: E/AndroidRuntime(30465): Caused by: java.lang.reflect.InvocationTargetException
09-15 18:05:04.239: E/AndroidRuntime(30465):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at java.lang.reflect.Method.invoke(Method.java:525)
09-15 18:05:04.239: E/AndroidRuntime(30465):    at android.view.View$1.onClick(View.java:3833)
09-15 18:05:04.239: E/AndroidRuntime(30465): Caused by: java.lang.NullPointerException
09-15 18:05:04.239: E/AndroidRuntime(30465):    at com.air.sprint123.Tut2.next2(Tut2.java:49)

3 个答案:

答案 0 :(得分:0)

试试这段代码

public void next2(View view) {
            mp.pause();
            mp.stop();
            mp.release();
            Intent intent = new Intent(this, Tut3.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);

        }

答案 1 :(得分:0)

由于您希望玩家在用户启动新活动时停止,因此您必须将代码放在onPause内,而不是unDestroy。请看这个图片,它是Android Activity LifeCycle:

enter image description here

答案 2 :(得分:0)

在导航到其他活动

之前,将其包含在next2()方法中
  if (mp1.isPlaying() && mp1!=null) {
                mp1.stop();
                mp1.reset();
                mp1.release();
            }

Check this Link for Reference