gpt4 book ai didi

android - 我无法在 android 中停止我的媒体播放器?

转载 作者:太空狗 更新时间:2023-10-29 15:27:30 24 4
gpt4 key购买 nike

我正在使用媒体播放器在计时器中播放滴答声。但它会在计时器结束后继续播放,即使在我停止播放媒体播放器之后也是如此。我在计时器的 ontick() 中播放了如下所示的声音。在 onfinish() 中调用了 mp.Stop(),但它不起作用。

              mp = MediaPlayer.create(getBaseContext(), sound);
mp.setOnCompletionListener(new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {
});
mp.setLooping(false);
// mp.setVolume(0.1, 0.1);
mp.start();
}

为我的计时器编码。

final class MyCounter extends CountDownTimer {

public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
timer.cancel();

MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.release();
mp.stop();
mp.reset();
}

}
};
mp.setOnCompletionListener(onCompletion);
i++;
submitAnswer();
final GameEngine ge = GameEngine.getInstance();

timeer.setText("Timer Completed.");

if (ge.getCurrentUnAllocatedAmount() > 0 && i <= 8) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
ge.setCurrentLevel(i);
startActivity(intent);
}
}, 2200);
} else if (ge.getCurrentUnAllocatedAmount() <= 0 || i > 8) {
Intent intent = new Intent(GameActivity.this,
ScoreActivity.class);
intent.putExtra("level", i);
startActivity(intent);
}

}

public void onTick(long millisUntilFinished) {
remTime = (millisUntilFinished / 1000);
timeer.setText("Time:" + (millisUntilFinished / 1000) + "");
mp = MediaPlayer.create(getBaseContext(), R.raw.beep7);
if (remTime > 0) {
mp.start();
mp.release();
}
if (remTime < 10) {
final Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
if (timeer.getVisibility() == View.VISIBLE) {
timeer.setVisibility(View.INVISIBLE);
} else {
timeer.setVisibility(View.VISIBLE);
}

timeer.setTextColor(Color.RED);

}
});
}
}
}

最佳答案

使用这段代码:

if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}

关于android - 我无法在 android 中停止我的媒体播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11469855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com