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

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

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

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

(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

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
//
1 Like

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

So this is the variable that stores the song file

AudioPlayer file;

And this is line is loading the file

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

And here is an image of the gui
player.PNG
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!

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

I don’t have more time than this

1 Like

Okay, thanks so much!

this file lister can sort


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
//
1 Like

when you have subfolders this maybe won’t work.

but see here: 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())

1 Like

OK, in setup() make the list.

You have one index, the current song number.

So on startup say

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

(I guess)


ON PREV

  // 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

  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() );
2 Likes

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

but see here: https://processing.org/examples/directorylist.html