# How to keep music from overlapping

**URL:** https://discourse.processing.org/t/how-to-keep-music-from-overlapping/15846
**Category:** Libraries
**Created:** [November 26, 2019, 9:46pm UTC](https://discourse.processing.org/t/how-to-keep-music-from-overlapping/15846 "2019-11-26T21:46:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Lane](https://avatars.discourse-cdn.com/v4/letter/l/779978/32.png) [@Lane](https://discourse.processing.org/u/Lane)
#### Post date: [November 26, 2019, 9:46pm UTC](https://discourse.processing.org/t/how-to-keep-music-from-overlapping/15846/1 "2019-11-26T21:46:07Z")

</div>

Hi! I have it where if the mouse is clicked inside the rectangle’s bounds then music will play. But every time I click inside the rectangle’s bounds the music will will repeat on top of each other. Is there a way I can get it where additional mouse clicks inside the bounds won’t result in overlapping music? I was thinking maybe there is a way to make the music to play out entirely before an additional mouse click plays the music again. Or if there was a way to make the music just restart if the mouse is clicked in the rectangle’s bounds after the initial click.

```auto
import processing.sound.*;
SoundFile file;

void setup() {
  size(500,500);
  file = new SoundFile(this,"music.mp3");
  
}

void draw() {
  fill(0);
  rect(width/2, height/2, 100,75);
}

void mouseClicked() {
  if (mouseX>(width/2) && mouseY>(height/2) && mouseY<(height/2+75) && mouseX<(width/2+100)) {
    file.play();;
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 26, 2019, 11:06pm UTC](https://discourse.processing.org/t/how-to-keep-music-from-overlapping/15846/2 "2019-11-26T23:06:28Z")

</div>

Add in a timer like this :

```auto
int timer = 0;

void mouseClicked() {
   if (mouseX.... && millis() > timer) {
      file.play();
      int soundDuration = int(file.duration()*1000);
      timer = millis()+soundDuration;
   }
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 9, 2019, 12:23am UTC](https://discourse.processing.org/t/how-to-keep-music-from-overlapping/15846/3 "2019-12-09T00:23:11Z")

</div>

> [@Lane](#):
>
> I was thinking maybe there is a way to make the music to play out entirely before an additional mouse click plays the music again.

@Lexyth gives a good solution here.

> [@Lane](#):
>
> Or if there was a way to make the music just restart if the mouse is clicked in the rectangle’s bounds after the initial click.

Check out the SoundFile reference:

> **[SoundFile / Libraries](https://processing.org/reference/libraries/sound/SoundFile.html)**
>
> This is a Soundfile player which allows to play back and manipulate sound files. Supported formats are: WAV, AIF/AIFF, and MP3. MP3 decoding can be very slow on ARM processors (Android/Raspberry Pi), …

Use `isPlaying()` to check if it is playing. If so, use `jump()` to jump to the beginning of the sound. Or `stop()` – whatever you want it to do.
