# A music player that automatically adds .wav or .mp3 files in the data folder to list?

**URL:** <https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473>\
**Category:** Libraries\
**Created:** [July 8, 2020, 11:55am UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473 "2020-07-08T11:55:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [July 8, 2020, 11:55am UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/1 "2020-07-08T11:55:52Z")

</div>

So I’m trying to make a music player using the minim library and here is the code:

```auto
import ddf.minim.analysis.*;
import ddf.minim.*;
 
BeatDetect heart = new BeatDetect();
Minim minim;
AudioPlayer file;
FFT fft;

int b1f = 0;

String text = "Nothing";
String theme = "bg (3).jpg";

PImage play;
PImage playPressed;
PImage pause;
PImage previous;
PImage next;
PImage pausePressed;
PImage loop;
PImage loopPressed;
PImage bg;
PImage bg2;
PImage bg3;
PImage bg4;
PImage settings;
PImage settingsPressed;
PImage cross;
PImage crossPressed;

PFont zigBlack;

boolean settingsb = false;
 
void setup(){
  size(600, 400);
 
  minim = new Minim(this);
  
  bg = loadImage(theme);
  
  zigBlack = createFont("Advent Pro Bold", 32);
  textFont(zigBlack);
  play = loadImage("play.png"); 
  playPressed = loadImage("playPressed.png");
  pause = loadImage("pause.png");
  previous = loadImage("previous.png");
  next = loadImage("next.png");
  pausePressed = loadImage("pausePressed.png");
  loop = loadImage("loop.png");
  loopPressed = loadImage("loopPressed.png");
  bg2 = loadImage("bg (2).jpg");
  bg3 = loadImage("bg (3).jpg");
  bg4 = loadImage("bg (4).jpg");
  settings = loadImage("settings.png");
  settingsPressed = loadImage("settingsPressed.png");
  cross = loadImage("cross.png");
  crossPressed = loadImage("crossPressed.png");

  file = minim.loadFile("Demons [Imagine Dragons].wav", 1024);
  fft = new FFT( file.bufferSize(), file.sampleRate() );
 
}
 
void draw() {
  background(255,170,0);
  
  stroke(0);
  image(bg,0,0,626,417);
  
  image(play,15,10,70,70);
  image(pause,65,120,50,50);
  image(previous,13,123,45,45);
  image(next,122,123,45,45);
  image(loop,538,120,50,50);
  image(settings,560,10,30,30);
  fill(0);
  rect(16,90,568,25,10);
  fill(255);
  textSize(20);
  text("Playing: " + text,22,109);
  
  if (mouseX<71 && mouseY<78 && mouseX>15 && mouseY>15) image(playPressed,15,10,70,70);
  if (mouseX<106 && mouseY<161 && mouseX>71 && mouseY>123) image(pausePressed,65,120,50,50);
  if (mouseX<585 && mouseY<159 && mouseX>547 && mouseY>125) image(loopPressed,538,120,50,50);
  if (mouseX<590 && mouseY<36 && mouseX>561 && mouseY>9) image(settingsPressed,560,10,30,30);

  fft.forward( file.right );
 
  for(int i = 0; i < fft.specSize(); i++)
  {
    fill(0);
    ellipse(i+300,300,2,fft.getBand(i)+2);
    ellipse(-i+300,300,2,fft.getBand(i)+2);
  }
  
  if (settingsb == true) {
    image(bg,0,0,626,417);
    image(cross,5,3,40,40);
    fill(b1f);
    rect(490,10,100,25,10);
    fill(0);
    strokeWeight(2);
    line(479,10,479,390);
    fill(255);
    textSize(20);
    text("Theme",515,30);
    image(bg2,55,100,104,69);
    image(bg3,185,100,104,69);
    image(bg4,315,100,104,69);
    fill(255);
    textSize(32);
    text("Choose a theme:",140,70);
  }
  
  if (settingsb == true && mouseX<40 && mouseY<36 && mouseX>10 && mouseY>9) image(crossPressed,5,3,40,40);
  
}
  
  void mouseReleased() {
  if (settingsb == false && mouseX<71 && mouseY<78 && mouseX>15 && mouseY>15 && file.isPlaying() == false)file.play();
  if (settingsb == false && mouseX<106 && mouseY<161 && mouseX>71 && mouseY>123 && file.isPlaying() == true)file.pause();
  if (settingsb == false && mouseX<585 && mouseY<159 && mouseX>547 && mouseY>125) file.loop();
  if (file.isPlaying() == true) text = "Demons [Imagine Dragons]";
  if (file.isPlaying() == false) text = "Nothing";
  if (mouseX<590 && mouseY<36 && mouseX>561 && mouseY>9) settingsb = true;
  if (settingsb == true && mouseX<40 && mouseY<36 && mouseX>10 && mouseY>9) settingsb = false;
  if (settingsb == true && mouseX<199 && mouseY<169 && mouseX>55 && mouseY>100) {
    theme = "bg (2).jpg"; 
    bg = loadImage(theme);}
  if (settingsb == true && mouseX<289 && mouseY<169 && mouseX>185 && mouseY>100) {
    theme = "bg (3).jpg";
    bg = loadImage(theme); }
  if (settingsb == true && mouseX<419 && mouseY<169 && mouseX>315 && mouseY>100) {
    theme = "bg (4).jpg";
    bg = loadImage(theme); }
}

```

You need to downlaod the minim library and these files to run:  
[https://drive.google.com/drive/folders/185IMGu0IYIR70NybM3xXD8WWTz5MO15A?usp=sharing](https://drive.google.com/drive/folders/185IMGu0IYIR70NybM3xXD8WWTz5MO15A?usp=sharing)

What I want to do is basically you drop .wav or .mp3 files to the data folder and it automatically adds them and shows the name of the file in front of “Now Playing:” when a file is playing. You can search through the list with the previous/next buttons (the buttons that the pause button is in between). But I don’t have much of an idea how I could do this or if this is possible. Do I need to use an arraylist?  
Thanks.

Also I saw this post which looks like exactly what I need but I ddin’t quite understand how it works: [https://forum.processing.org/one/topic/lovely-mp3-player.html](https://forum.processing.org/one/topic/lovely-mp3-player.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 4:12pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/2 "2020-07-08T16:12:53Z")

</div>

(actually the song player is by me)

**File lister**

here is an example to register new files throughout

you have to

- select wav/mp3
- sort

Chrisir

```auto
String[] fileList=null; 

//---------------------------------------------------------------------------
// processing core functions 

void setup() {
  size(880, 800);
  background(111); // gray
}

void draw() {
  background(111); // gray
  updateList(); 
  showList();
}

//---------------------------------------------------------------------------
// Other functions 

void updateList() {
  File file = new File(dataPath(""));
  fileList = file.list();

  if (fileList==null) {
    println("Folder is empty.");
  } else {
    // success
  }//else
}//func 

void showList() {
  if (fileList==null) 
    return; // leave 

  int i=0; 
  for (String s1 : fileList) { 
    text(s1, 33, 44+i*22);
    i++;
  }//for
  //
}//func
//

```

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [July 8, 2020, 4:43pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/3 "2020-07-08T16:43:21Z")

</div>

That is really helpful thank you so much!  
But I also need help with the sorting 😃

So this is the variable that stores the song file

```auto
AudioPlayer file;

```

And this is line is loading the file

```auto
file = minim.loadFile("Demons [Imagine Dragons].wav", 1024);

```

And here is an image of the gui  
 ![player.PNG](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f0b84c7fd46cf5b5a830f0e1a9025e58ffa51dc1.jpeg)  
There is previous/next buttons at the right and left of the pause button. I want the file variable to load the first file in the list when the player starts and when clicked on next it goes to the next file in the list. Same with the previous button.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 5:08pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/4 "2020-07-08T17:08:59Z")

</div>

GUI looks great

my sketch above checks the folder while we work, so you can drop a new file in it and the Sketch while running registers it

see SongPlay12 here by the way  
see [https://github.com/Kango/Processing-snippets](https://github.com/Kango/Processing-snippets)

I don’t have more time than this

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [July 8, 2020, 5:18pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/5 "2020-07-08T17:18:24Z")

</div>

Okay, thanks so much!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 5:19pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/6 "2020-07-08T17:19:23Z")

</div>

this file lister can sort

```auto

StringList inventory;  

//---------------------------------------------------------------------------
// processing core functions 

void setup() {
  size(880, 800);
  background(111); // gray
}

void draw() {
  background(111); // gray
  updateList(); 
  showList();
}

//---------------------------------------------------------------------------
// Other functions 

void updateList() {
  File file = new File(dataPath(""));
  String[] fileList = file.list();

  if (fileList==null) {
    println("Folder is empty.");
  } else {
    // success
    inventory = new StringList();
    for (String s1 : fileList) { 
      inventory.append(s1);
    }

    inventory.sort();
  }//else
}//func 

void showList() {
  if (inventory==null) 
    return; // leave 

  int i=0; 
  for (String s1 : inventory) { 
    text(s1, 33, 44+i*22);
    i++;
  }//for
  //
}//func
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 5:26pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/7 "2020-07-08T17:26:53Z")

</div>

when you have subfolders this maybe won’t work.

but see here: [https://processing.org/examples/directorylist.html](https://processing.org/examples/directorylist.html)

for long lists of files this is time consuming.

Instead you could just check the number of files and only when the number changes, read the entire folder again.

So you only do a full update when the Sketch starts (in setup())

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 5:36pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/8 "2020-07-08T17:36:46Z")

</div>

> [@qewer3322](#):
>
> You can search through the list with the previous/next buttons

OK, in setup() make the list.

You have one index, the current song number.

So on startup say

```auto
  file = minim.loadFile( inventory.get(currentSongNumber), 1024);
  fft = new FFT( file.bufferSize(), file.sampleRate() );

```

(I guess)

* * *

**ON PREV**

```auto
  // some players restart current song with prev and when you click prev again, go prev song
  currentSongNumber--; if(currentSongNumber<0) currentSongNumber=0; 
  file = minim.loadFile( inventory.get(currentSongNumber), 1024);
  fft = new FFT( file.bufferSize(), file.sampleRate() );

```

**ON NEXT**

```auto
  currentSongNumber++; 
  // check if end of list / either go 0 or do nothing
  file = minim.loadFile( inventory.get(currentSongNumber), 1024);
  fft = new FFT( file.bufferSize(), file.sampleRate() );

```

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [July 8, 2020, 6:03pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/9 "2020-07-08T18:03:01Z")

</div>

It works nice without a subfolder but doesn’t works with one. Thanks so much for helping! Really appreciate it.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2020, 6:05pm UTC](https://discourse.processing.org/t/a-music-player-that-automatically-adds-wav-or-mp3-files-in-the-data-folder-to-list/22473/10 "2020-07-08T18:05:24Z")

</div>

but see here: [https://processing.org/examples/directorylist.html](https://processing.org/examples/directorylist.html)
