# MouseClick the screen to play music randomly

**URL:** <https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909>\
**Category:** Libraries\
**Created:** [June 16, 2020, 1:54am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909 "2020-06-16T01:54:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![beryl1997](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@beryl1997](https://discourse.processing.org/u/beryl1997)\
**Post date:** [June 16, 2020, 1:54am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/1 "2020-06-16T01:54:37Z")

</div>

Now I have 15 pieces of music (15 files), I hope that when the mouse clicks on the screen, these 15 pieces of music can be played randomly  
Now I only know what to do when playing one piece of music…  
This is the current code to play a music file when the mouse is clicked：

import processing.sound.\*;  
SoundFile s;  
void mousePressed() {  
s= new SoundFile(this,"…/byzp/data/666.aiff");  
s.play();  
}

If anyone knows how to play random music when mouse clicks on the screen, please help me, thank you very much 😃

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 16, 2020, 3:27am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/2 "2020-06-16T03:27:30Z")

</div>

add your sounds to an array.  
`SoundFile[] mySounds = new SoundFile[numberOfSounds];`

create an `int` to store your random number.  
`int myRandomNumber = 0;`

update random number when mouse is pressed.  
`myRandomNumber = int(random(mySounds.length-1));`

play the sound stored in the relative array index.  
`mySounds[myRandomNumber].play();`

---

<div class="post-metadata">

**Author:** ![beryl1997](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@beryl1997](https://discourse.processing.org/u/beryl1997)\
**Post date:** [June 16, 2020, 3:30am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/4 "2020-06-16T03:30:01Z")

</div>

Thank you very much 😄

---

<div class="post-metadata">

**Author:** ![beryl1997](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@beryl1997](https://discourse.processing.org/u/beryl1997)\
**Post date:** [June 16, 2020, 3:57am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/5 "2020-06-16T03:57:02Z")

</div>

Hello, I would like to ask, how to import multiple music files in processing?Thank you~

---

<div class="post-metadata">

**Author:** ![slow\_izzm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/slow_izzm/32/5637_2.png) [@slow\_izzm](https://discourse.processing.org/u/slow_izzm)\
**Post date:** [June 16, 2020, 4:35am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/6 "2020-06-16T04:35:51Z")

</div>

You can either add them individually …

`mySounds[0] = loadSound(this, path);`  
`mySounds[1] = loadSound(this, path);`

or use a `for loop` …

```auto
for (int i = 0; i < numberOfSounds; i++) {
  mySounds[i] = loadSound(this, path + i + format);
}

```

the latter you would use a naming convention such as `snd0.mp3`, `snd1.mp3`, ect …

---

<div class="post-metadata">

**Author:** ![beryl1997](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@beryl1997](https://discourse.processing.org/u/beryl1997)\
**Post date:** [June 16, 2020, 6:07am UTC](https://discourse.processing.org/t/mouseclick-the-screen-to-play-music-randomly/21909/7 "2020-06-16T06:07:41Z")

</div>

Thanks for your help~
