# Timer that ends with a song

**URL:** https://discourse.processing.org/t/timer-that-ends-with-a-song/8270
**Category:** Libraries
**Created:** [February 10, 2019, 4:55pm UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270 "2019-02-10T16:55:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![juliastar](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@juliastar](https://discourse.processing.org/u/juliastar)
#### Post date: [February 10, 2019, 4:55pm UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270/1 "2019-02-10T16:55:25Z")

</div>

Hi,

I’m very new to processing. Right now I’m trying to create a game for kids. The thing is that I’m trying to create a timer and when it is equal to 0 a song will go off. So thanks to this forum and Youtube I manage to create a code that now plays the music (unfortunately the song goes off right away) and my timer counts down from 10, tho it doesn’t stop at 0. Do you know what I should do?

**Klocka**

```auto
Timer startTimer;

import ddf.minim.*;

AudioPlayer player;
Minim minim;//audio context

void setup()
{
  size(600, 600);
  background(255);
  startTimer = new Timer(10); 
  
  minim = new Minim(this);
  player = minim.loadFile("old.mp3", 2048);
  player.play();

}

void draw()
{
  background(200);
  startTimer.countDown();
  fill(0);
  text(startTimer.getTime(),20,20);
}
  
void stop()
{
  player.close();
  minim.stop();
  super.stop();
}

**Timer**

class Timer 
{
  float Time;
  Timer(float set) 
  {
    Time = set;
  }
  float getTime() 
  {
    return(Time);
  }
  void setTimer(float set) 
  {
    Time = set;
  }
  
  void countDown() 
  {
    Time -= 1/frameRate;
  }

}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [February 11, 2019, 3:35pm UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270/2 "2019-02-11T15:35:51Z")

</div>

Hey,

You can format your code by :

- Pressing `Ctrl+T` in the Processing IDE code editor.
- Using the button `</>` in the message editor on this forum.

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [February 11, 2019, 6:00pm UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270/3 "2019-02-11T18:00:23Z")

</div>

> [@](#):
>
> when it is equal to 0 a song will go off

Rather than playing the player as soon as the sketch runs, play it in draw loop when the timer reaches 0.

```auto
if(startTimer.getTime() <= 0){ 
    player.play();
  }

```

* * *

> [@](#):
>
> stop at 0

Only decrease the value of the timer if it is greater than 0. (And if it tries to go to less than 0 snap it back to 0.)

```auto
if(Time > 0){
      Time -= 1/frameRate;
      if(Time < 0) Time = 0;
    }
}

```

---

<div class="post-metadata">

### Author: ![juliastar](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@juliastar](https://discourse.processing.org/u/juliastar)
#### Post date: [February 12, 2019, 7:54am UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270/4 "2019-02-12T07:54:54Z")

</div>

> [@TheWizardBear](#):
>
> if(startTimer.getTime() \<= 0){ player.play(); }

Thank you! It now works 🙂

---

<div class="post-metadata">

### Author: ![juliastar](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@juliastar](https://discourse.processing.org/u/juliastar)
#### Post date: [February 12, 2019, 7:56am UTC](https://discourse.processing.org/t/timer-that-ends-with-a-song/8270/5 "2019-02-12T07:56:28Z")

</div>

Thank you for the tip! 🙂
