我正在使用媒体播放器在计时器中播放滴答声。但是在计时器结束后它继续播放,甚至在我为媒体播放器调用停止之后。我在定时器的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);
}
});
}
}
}
答案 0 :(得分:0)
使用此代码:
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
答案 1 :(得分:0)
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
});
答案 2 :(得分:0)
试试这个:
mp.setOnCompletionListener(onCompletion);
private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if(mp.isPlaying())
{
mp.stop();
mp.reset();
}
}
};
答案 3 :(得分:0)
我认为您没有在release()
本身上调用onCompletionListener()
方法。
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
});
答案 4 :(得分:0)
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
mp = null;
}
});
答案 5 :(得分:-1)
计时器停止时检查条件。
if(mPlayer.isPlaying())
{
mPlayer.stop();
}