MouseClick the screen to play music randomly

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");;

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

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.

1 Like

Thank you very much :smile:

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

You can either add them individually …

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

or use a for loop

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 …

1 Like

Thanks for your help~