媒体播放器在退出应用程序或电话铃声时停止播放歌曲

时间:2014-04-14 16:46:10

标签: android eclipse android-mediaplayer

当我按下主页按钮或接听电话时,我的歌不停!歌曲停止时我的计时器也不停止?有没有办法可以阻止我的计时器?你能帮我吗?

public class MainActivity extends Activity {

    private Button start, stop;
    private MediaPlayer mp;
    private TextView display, comment;

    private TextView timerValue;

    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        timerValue = (TextView) findViewById(R.id.timerValue);
        start = (Button) findViewById(R.id.bStart);
        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                mp = MediaPlayer.create(MainActivity.this, R.raw.splahsound);
                mp.start();
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {

                        mp.stop();
                        mp.reset();
                        mp.release(); // free up memory
                        mp = null;

                    }

                });

            }
        });

        stop = (Button) findViewById(R.id.bDur);
        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mp != null) {
                    try {

                        mp.stop();
                        mp.reset();
                        mp.release();
                        mp = null;

                    } catch (Exception e) {
                        Log.d("error", e.toString());
                    }
                }
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
                            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();       
    }

    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));
            customHandler.postDelayed(this, 0);
        }

    };

}

2 个答案:

答案 0 :(得分:2)

您可以在onPause方法中停止MediaPlayer:

@Override
protected void onPause() {
    //stop mediaplayer:
    if (mp != null && mp.isPlaying()) {
        mp.stop();
    }
    super.onPause();       
}

而不是:

@Override
protected void onPause() {
    super.onPause();       
}

注意:检查MediaPlayer当前是否正在播放很重要,因为在当前未播放的MediaPlayer上调用stop()方法会抛出IlegalStateException。

答案 1 :(得分:0)

当你按下Home按钮

时,你需要覆盖onPause和onResume
@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first
    mp.pause();
    // stop the clock
}

@Override
public void onResume() {
    super.onPause();  // Always call the superclass method first
    mp.start();
    // re-sync the clock with player...
}

如果您在手机响铃且应用程序在后台运行时需要停止任何操作,则需要将您的应用订阅到PHONE_STATE,请参阅this link