【问题标题】:How do I stop MediaPlayer sound while the App is closed/minimized?如何在应用程序关闭/最小化时停止 MediaPlayer 声音?
【发布时间】:2015-03-24 22:07:57
【问题描述】:

在我的秒表应用程序中,开始按钮应该开始声音,而暂停按钮应该停止声音。到目前为止,我的程序运行良好。

但在播放声音的过程中,如果我返回或最小化应用程序,声音不会停止。它一直在播放(一直播放,即使设备处于空闲状态)。奇怪的是,当我重新打开应用程序停止声音时,它永远不会停止。 如何解决?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timerValue = (TextView) findViewById(R.id.timerValue);

    startButton = (Button) findViewById(R.id.startButton);
    mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

               mp.start();
                mp.setLooping(true);


        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
            if(mp.isPlaying())
            {
                mp.pause();

            }
       }
    });
    resetButton = (Button) findViewById(R.id.reset);


    resetButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timerValue.setText("" + 00 + ":"
                    + String.format("%02d", 00) + ":"
                    + String.format("%03d", 00));
            startTime = SystemClock.uptimeMillis();
            timeSwapBuff = 0;

        }
    });

}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};

【问题讨论】:

    标签: android android-activity android-mediaplayer android-button


    【解决方案1】:

    您可以使用 Android 生命周期。

    我想你可以在onStop()onDestroy() 上致电mp.pause();

    示例代码:

    @Override
        protected void onStop() {
            super.onPause();
            mp.pause();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            mp.pause();
        }
    

    【讨论】:

      【解决方案2】:

      您应该访问相同的 MediaPlayer 对象
      公开,也许可行
      这个解决方案对我有用:创建一个类并定义静态 MediaPlayer 对象和静态方法(暂停、停止等)
      您也可以在活动类中覆盖 onPause 方法并停止媒体播放器

      【讨论】:

        【解决方案3】:

        要停止播放,请直接在 onPause() 函数上调用 mp.pause()。但是,您的 onStop() 应该看起来更像:

        @Override
            protected void onStop() {
                super.onStop();        //  <<-------ENSURE onStop()
                mp.stop();
                mp.release();
            }
        

        如果您的设备中有 MENU 和 HOME 按钮,您应该检查您的应用在按下这些按钮后是否恢复。

        Kf

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多