Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发布mediaplayer onDestroy android_Android - Fatal编程技术网

发布mediaplayer onDestroy android

发布mediaplayer onDestroy android,android,Android,每当用户打开另一个活动时,我都试图停止mp3。然而,我使用的代码似乎并没有停止mp3 public class Tut2 extends Activity { private MediaPlayer mp1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.

每当用户打开另一个活动时,我都试图停止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) {
            mp.pause();
            mp.stop();
            mp.release();
            Intent intent = new Intent(this, Tut3.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);

        }
试试这个代码

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

        }

由于您希望玩家在用户启动新活动时停止,因此必须将代码放在onPause中,而不是放在Understroy中。请看这张图片,它是Android活动生命周期:


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


在导航到其他活动之前,请将其包含在您的
next2()
方法中

  if (mp1.isPlaying() && mp1!=null) {
                mp1.stop();
                mp1.reset();
                mp1.release();
            }

在导航到其他活动之前,请将其包含在您的
next2()
方法中

  if (mp1.isPlaying() && mp1!=null) {
                mp1.stop();
                mp1.reset();
                mp1.release();
            }

您还应该在
mp1.stop()
之前检查
mp1.isplay()
,否则您可能会得到
IlligalStateException
也可以在
next2
方法中添加此代码。您还应该在
mp1.stop()之前检查
mp1.isplay()
否则您可能会得到
IlligalStateException
并在
next2
方法中添加此代码。情况并非如此,因为我在第一个活动中使用了相同的代码,并且工作没有问题,而且我已经尝试暂停。情况并非如此,因为我在第一个活动中使用了相同的代码,并且工作没有问题问题,我已尝试暂停。问题中有此问题,请检查它声明
MediaPlayer mp1
全局无最终关键字,并在
onCreate
方法
mp1=MediaPlayer.create(getBaseContext(),R.raw.plan_one)中初始化它
然后在
next2
方法中尝试三行代码这次应用程序没有强制关闭,但我仍然可以听到mp3,并且它没有停止在
onDestroy()
方法中写入相同的代码。如果有问题,请检查它声明
MediaPlayer mp1
全局无最终关键字,并在
onCreate
方法
mp1=MediaPlayer.create(getBaseContext(),R.raw.plan_one)中初始化它
然后在
next2
方法中尝试三行代码这次应用程序没有强制关闭,但我仍然可以听到mp3,而且它没有停止在
onDestroy()方法中写入相同的代码