我可以在活动暂停时成功取消倒数计时器。但是,在取消定时器的OnTick方法中实现的媒体播放器不会停止5-8秒。当活动暂停时,在CountDown计时器中禁用媒体播放器声音的正确方法是什么?
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();
}
}
我几乎100%肯定倒数计时器在活动暂停时被取消,因为onFinish()函数中的媒体播放器从未被调用过。为什么onTick方法中的媒体播放器会继续播放?
答案 0 :(得分:0)
我明白了:要取消CountDown计时器中的媒体播放器,当活动暂停时......您必须将MediaPlayer声明为静态,而不是最终。
{{1}}