倒计时结束前播放声音

发布于 2025-01-06 13:09:57 字数 643 浏览 1 评论 0原文

您好,这里是一段代码...想法是它运行 20 秒,然后在 0 之前的 5 秒播放一个声音,然后在倒计时结束时播放下一个声音...但它只是播放结尾并跳过5 秒标记...我尝试过使用它,但无法让它工作。

有什么想法吗?

//Counter 2
final CountDownTimer counter2 = new CountDownTimer(20000 , 1000) {
public void onTick(long millisUntilFinished) {
  mCounter2TextField.setText(" " + formatTime(millisUntilFinished));

  long timeLeft = millisUntilFinished / 1000;

  if (timeLeft == 5000) 
         playAlertSound(R.drawable.sound1);

}

public void onFinish() {
  start();
  playSound();
}

public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
mp.start();

}   


};

Hi here is a snipit of code...the idea is that it runs for 20 secs then at 5 seconds before 0 it plays one sound then at end of countdown plays the next...but it is just playing the end and skipping the 5 sec mark...I have tried playing with it but can't get it to work.

Any ideas?

//Counter 2
final CountDownTimer counter2 = new CountDownTimer(20000 , 1000) {
public void onTick(long millisUntilFinished) {
  mCounter2TextField.setText(" " + formatTime(millisUntilFinished));

  long timeLeft = millisUntilFinished / 1000;

  if (timeLeft == 5000) 
         playAlertSound(R.drawable.sound1);

}

public void onFinish() {
  start();
  playSound();
}

public void playSound() {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
mp.start();

}   


};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云悠悠 2025-01-13 13:09:57

这是因为您的 :

playSound(...);

与您的计时器位于同一“线程”中。你应该这样做:

if (timeLeft == 5000) 

  new Runnable(){
      void Run(){
           playAlertSound(R.drawable.sound1);
      }
      public void playSound() {
            MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
            mp.start();
       }

  }.run();

或者类似的事情:D

因为你不能减少计时器并同时播放声音! :)

It's cause your :

playSound(...);

Is in the same 'thread' as your Timer. You should just do somthing like :

if (timeLeft == 5000) 

  new Runnable(){
      void Run(){
           playAlertSound(R.drawable.sound1);
      }
      public void playSound() {
            MediaPlayer mp = MediaPlayer.create(getBaseContext(), (R.drawable.sound2));
            mp.start();
       }

  }.run();

Or something like that :D

Cause you cant decrease your Timer and play sound at same time ! :)

够钟 2025-01-13 13:09:57

您将 millisUntilFinished 转换为 timeLeft 中的秒,然后告诉它仅在还剩 5000 秒(而不是毫秒)时播放。

当您进行更改来解决此问题时,您还应该更改与 timeLeft <= 5 的比较,以便在使用 调用 onTick 时它将开始播放还剩 4999 毫秒。当然,如果声音已经开始播放,您需要添加逻辑以不播放声音。

You're converting millisUntilFinished to seconds in timeLeft, and then telling it to only play if there are 5000 seconds (not milliseconds) left.

As you're making the change to fix that, you should also change the comparison to timeLeft <= 5 so it will begin playing if onTick is called with 4999 milliseconds left. You'll, of course, need to add logic to not play the sound if it's already been started playing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文