Minim: Play a random song out of an array

try this code

// https://discourse.processing.org/t/minim-play-a-random-song-out-of-an-array/14683

import ddf.minim.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer[] playlistGood;

void setup() {
  size(800, 800, P3D);
  minim = new Minim(this);
  playlistGood = new AudioPlayer[2];
  playlistGood[0] = minim.loadFile("groove0.mp3");
  playlistGood[1] = minim.loadFile("groove1.mp3");

  for ( int i = 0; i < playlistGood.length; i++ ) {
    playlistGood[i].loop();
    playlistGood[i].pause();
  }
}

// button config
int b1x=700, b1y=100, b1w=80, b1h=30;
boolean set1 = false;
int b2x=700, b2y=200, b2w=80, b2h=30;
boolean set2 = false;
// mouse over rect function
boolean over(int x, int y, int w, int h) {
  if (  
    ( mouseX > x ) && ( mouseX < ( x + w ) ) && 
    ( mouseY > y ) && ( mouseY < ( y + h ) ) 
    )  return true;
  return false;
}

void draw_button() {
  strokeWeight(3);
  // button 1
  if ( over(b1x, b1y, b1w, b1h) ) stroke(200, 0, 200);     // border color
  else                            stroke(0, 200, 200);
  if ( set1 )   fill(0, 200, 0);                           // status fill color
  else          fill(0, 0, 200);
  rect(b1x, b1y, b1w, b1h);
  // button 2
  if ( over(b2x, b2y, b2w, b2h) ) stroke(200, 0, 200);     // border color
  else                            stroke(0, 200, 200);
  if ( set2 )   fill(0, 200, 0);                           // status fill color
  else          fill(0, 0, 200);
  rect(b2x, b2y, b2w, b2h);
}

void draw() {
  background(200, 200, 0);
  draw_button();
}

void mousePressed() {
  if ( over(b1x, b1y, b1w, b1h) ) {                          // button 1
    set1 = ! set1;
    if ( set1 ) {
      set2 = false;                                          // make option group logic unset others
      play_only_one(0);
    } else {
      pause_only_one(0);
    }
  }
  if ( over(b2x, b2y, b2w, b2h) ) {                          // button 2
    set2 = ! set2;
    if ( set2 ) {
      set1 = false;                                          // make option group logic unset others
      play_only_one(1);
    } else {
      pause_only_one(1);
    }
  }
}

void play_only_one(int j ) {
  for ( int i = 0; i < playlistGood.length; i++ ) {
    if ( ! playlistGood[i].isPlaying() && ( i == j ) ) {
      playlistGood[i].loop();  // if not play, start this one
      println("start "+j);
    } else playlistGood[i].pause();                                               // stop the rest
  }
}

void pause_only_one(int j ) {
  if ( playlistGood[j].isPlaying() ) {
    playlistGood[j].pause();
    println("stop "+j);
  }
}


i did NOT use class …
and only the song array ( like you have )
so the buttons are very manual…
but as a first step easy to understand, esp note the OVER function

it is not a random song play, it is a idea for one button for one song structure…
so for your idea need to change.
i wanted to show better buttons and the play one song logic.

1 Like