简体   繁体   English

暂停活动后,如何在倒数计时器中正确停止Media Player?

[英]How do I correctly stop a Media Player in a CountDown Timer when the activity is Paused?

I can successfully cancel the Countdown Timer before it finishes when the activity is paused. 在活动暂停时,我可以在倒数计时器完成之前成功取消它。 However, the media player implemented in the OnTick method of the canceled timer doesn't stop for 5-8 seconds. 但是,在已取消计时器的OnTick方法中实现的媒体播放器不会停止5-8秒。 What would be the correct way to disable the sound of the media player in the CountDown Timer when the activity is paused? 当活动暂停时,在倒数计时器中禁用媒体播放器声音的正确方法是什么?

private Button soundOn;
private Button soundOff;

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



   SharedPreferences savedSoundToggle = getSharedPreferences(PREFS_SOUNDTOGGLE, 0);
    final SharedPreferences.Editor soundEditor = savedSoundToggle.edit();

    nSound = savedSoundToggle.getInt("SoundOnorOff", 0);


    if(nSound == 1){
        soundOff.setEnabled(false);
        soundOff.setVisibility(View.INVISIBLE);
        soundOn.setEnabled(true);
        soundOn.setVisibility(View.VISIBLE);
    }
    else if( nSound == 0){
        soundOn.setEnabled(false);
        soundOn.setVisibility(View.INVISIBLE);
        soundOff.setEnabled(true);
        soundOff.setVisibility(View.VISIBLE);
    }

}

public void OneGame(){

  final MediaPlayer intro = MediaPlayer.create(getApplicationContext(), R.raw.minicycleaudio);
  final MediaPlayer go = MediaPlayer.create(getApplicationContext(), R.raw.flashgoaudio);

  getReadyTimer = new CountDownTimer(6000, 1000) {

        public void onTick(long millisUntilFinished) {

            getReadyTimeText.setText("" + millisUntilFinished / 1000);

            if(soundOn.isEnabled()){
                intro.start();
            }
            if(soundOff.isEnabled()){
                if(intro.isPlaying()){
                    intro.pause();
                }
            }
         }

          public void onFinish() {

            if(intro.isPlaying()) {
                intro.stop();
                intro.release();
            }
             if(soundOn.isEnabled()) {
                go.start();
            }


@Override
public void onPause() {

  super.onPause();
  getReadyTimer.cancel();

  }
}

I'm almost 100% positive that the Countdown timer becomes canceled when the activity is paused because the media player in the onFinish() function is never called. 我几乎100%地肯定,由于从未调用过onFinish()函数中的媒体播放器,因此在活动暂停时,倒数计时器将被取消。 Why does the media player in the onTick method keep playing? 为什么onTick方法中的媒体播放器继续播放?

I figured it out: To cancel a media player in a CountDown Timer, when the activity is paused... you must declare the MediaPlayer as static, not final. 我弄清楚了:若要在活动停止时在CountDown计时器中取消媒体播放器,则必须将MediaPlayer声明为静态的,而不是最终的。

static  MediaPlayer intro;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level_one);
    getReadyTimeText = (TextView) findViewById(R.id.levelonetimerText);

 public void OneGame(){
    intro = MediaPlayer.create(getApplicationContext(), R.raw.minicycleaudio);


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM