# Random mp3 files

**URL:** <https://discourse.processing.org/t/random-mp3-files/26346>\
**Category:** Libraries\
**Created:** [December 17, 2020, 9:04pm UTC](https://discourse.processing.org/t/random-mp3-files/26346 "2020-12-17T21:04:09Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:04pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/1 "2020-12-17T21:04:09Z")

</div>

I’m trying to make a random generator of mp3 files on Processing but I don’t know how to do it.  
I’ve tried to create a class (and before I tried it with an Array) with my audio files but I can’t get it to run.

> import processing.sound.\*;  
> processing\_merci.Merci.files;  
> SoundFile file;  
> ArrayList sound\_merci = new ArrayList();  
> void setup() {  
> size (800, 800);  
> background(255);  
> file.play();  
> }  
> void draw() {  
> }

and here’s my class:

> import processing.sound.\*;  
> class Merci {  
> // définir les attributs  
> SoundFile files;  
> ArrayList files = new ArrayList();  
> Merci (){  
> files = new SoundFile(this, “merci\_Victor.mp3”);  
> file = new SoundFile(this, “merci\_Ayten.mp3”);  
> file = new SoundFile(this, “merci\_JulieT.mp3”);  
> file = new SoundFile(this, “merci\_Lenard.mp3”);  
> file = new SoundFile(this, “merci\_Lisa.mp3”);  
> file = new SoundFile(this, “merci\_Noé.mp3”);  
> file = new SoundFile(this, “merci\_Pierre.mp3”);  
> file = new SoundFile(this, “merci\_Rita.mp3”);  
> file = new SoundFile(this, “merci\_Prune.mp3”);  
> file = new SoundFile(this, “merci\_Sarah.mp3”);  
> file = new SoundFile(this, “merci\_Thomas.mp3”);  
> file = new SoundFile(this, “merci\_Vlad.mp3”);  
> }  
> }

😬

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:12pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/2 "2020-12-17T21:12:43Z")

</div>

so you want to play a random mp3 file from the collection?

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:13pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/3 "2020-12-17T21:13:54Z")

</div>

yes exactly ! I would like a random file from my collection to be played with each mouse click. 🙂

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:14pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/4 "2020-12-17T21:14:26Z")

</div>

I’ve done it before but I’m not sure if it will work to you. Give me a minute, I’ll send you the code

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:15pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/5 "2020-12-17T21:15:13Z")

</div>

so sweet thank you very much !!

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:17pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/6 "2020-12-17T21:17:24Z")

</div>

```auto
int ALL = 9; //audio library length
int Psong = int(random(ALL)); //playing song
float SInfo[][] = new float[ALL][1]; //[X][] - song ID, [][X] - 0: allowed (-1 banned, 1 allowed)

import ddf.minim.*;
Minim minim;

AudioPlayer songs[] = new AudioPlayer[ALL];

void setup() {
  minim = new Minim(this);
  for(int i = 0; i < ALL; i++) {
    songs[i] = minim.loadFile("SONGS/"+i+".mp3");
    SInfo[i][0] = 1;
  }
}

void draw() {
  if(!songs[Psong].isPlaying()) {
    Psong++;
    Psong = Psong % ALL;
    songs[Psong].rewind();
    songs[Psong].play();
  }
}

//void nextSong() {
// Psong++;
// for(int i = 0; i < ALL*2 && SInfo[i][0] != 1; i++) {
// Psong++;
// Psong = Psong % ALL;
// }
//}

```

the songs are name 0.mp3, 1.mp3 … 9.mp3

edit: this is the first version. I’ll try to find a better one but don’t get your hopes up.

edit2: I’ve done this when I didn’t know much ArrayLists. It would be easier to do it with them probably but I’m too lazy to try

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:19pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/7 "2020-12-17T21:19:19Z")

</div>

I’m going to watch and try it all thanks a lot

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:23pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/8 "2020-12-17T21:23:09Z")

</div>

I found the better version. The banning of songs actually works. There is also a “hidden song” which only plays if the MODE == 1. Have fun!

```auto
int ALL = 9; //audio library length
float SInfo[][] = new float[ALL][1]; //[X][] - song ID, [][X] - 0: allowed (-1 banned, 1 allowed)
int MODE = 1;
int Psong = (MODE == 0)? (int)random(ALL) : 9; //playing song

import ddf.minim.*;
Minim minim;
AudioPlayer songs[] = new AudioPlayer[ALL+1];

void setup() {
  minim = new Minim(this);
  for (int i = 0; i < ALL; i++) {
    songs[i] = minim.loadFile("SONGS/"+i+".mp3");
    SInfo[i][0] = 1;
  }
  songs[9] = minim.loadFile("SONGS/BL.mp3");
}

void draw() {
  if (!songs[Psong].isPlaying()) {
    Psong++;
    Psong = Psong % ALL;
    if (MODE == 1) {
      Psong = ALL;
    }
    songs[Psong].rewind();
    songs[Psong].play();
  }
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:24pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/9 "2020-12-17T21:24:57Z")

</div>

If you have any way for me to send you the whole file (with songs) then tell me if you want. I can’t post it here since it is .zip well yeah

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:28pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/10 "2020-12-17T21:28:37Z")

</div>

Thank you I’m trying to play it but I arrive to an error NullPointerException 🤐

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:29pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/11 "2020-12-17T21:29:15Z")

</div>

it’s because you dont have the files

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:29pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/12 "2020-12-17T21:29:26Z")

</div>

Oh that would be really nice  
I could send you by mail ? or another plateform?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:29pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/13 "2020-12-17T21:29:36Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/372747315c868f9d62cfececbc5e48ef1468609e.png)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:30pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/14 "2020-12-17T21:30:13Z")

</div>

what do you suggest? I have a few throw-away gmails so we can do that

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:31pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/15 "2020-12-17T21:31:59Z")

</div>

What is throw-away gmails ? Is ather gmail boy to stay anonymous ?

---

<div class="post-metadata">

**Author:** ![toc\_toc\_toc](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@toc\_toc\_toc](https://discourse.processing.org/u/toc_toc_toc)\
**Post date:** [December 17, 2020, 9:32pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/16 "2020-12-17T21:32:16Z")

</div>

but yes that’s a good idea I thing 🙂

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 17, 2020, 9:33pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/17 "2020-12-17T21:33:02Z")

</div>

yes. I use it to sign up for some websites that spam notifications so I don’t have the issue on my personal mail. It’s also good for anonymity. EDIT: you should have receved my DM

---

<div class="post-metadata">

**Author:** ![markcosmic](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@markcosmic](https://discourse.processing.org/u/markcosmic)\
**Post date:** [December 24, 2020, 6:34pm UTC](https://discourse.processing.org/t/random-mp3-files/26346/18 "2020-12-24T18:34:41Z")

</div>

What I would do is to create a CSV file which contains the names and locations of my songs. Then read the CSV using table() methods into an array. Then randomly access the array index. Load and play. This allows you to change the list of songs without updating code. I’ve had trouble using the sound() library which is easy to use, but minim works well. I’ll write the code and send it to you if you like
