playing random sounds in array

How do I play multiple tracks randomly from an array?

I have this code thats works but it only plays one track:

Minim minim;
AudioPlayer song;

Serial myPort; // The serial port
PFont myFont; // The display font
char inString; // Input string
int lf = 10; // ASCII linefeed

void setup() {
size(400, 200);
minim = new Minim(this);
song = minim.loadFile("track1.mp3");

}

void draw() {

while (inString == 'O' && !song.isPlaying ()) {

play();
}
if (song.isPlaying()) {
// song.close();
// minim.stop();
// super.stop();
}

delay(200);
}

void play() {
if (!song.isPlaying()) {
// The play() function plays the sound, as long as it is not already playing.
// rewind() ensures the sound starts from the beginning.
song.rewind();
song.play();
}
}

void serialEvent(Serial myPort) {
inString = myPort.readChar();
print(inString);
}
void stop()
{
song.close();
minim.stop();

super.stop();
}

Very grateful for any help, thanks!

Can you pick out the parts of that code that cause a song object to be available for use?

Any type that you can create one of you can create an array of.

Processing has a random number function. Use that to get a random number between 0 and the number of AudioPlayer objects you have in the array, and use that value as the index into the array.

I googled for 'processing minim array'. The results suggest that you can create an array of AudioPlayer objects.

PaulS:
Can you pick out the parts of that code that cause a song object to be available for use?

Any type that you can create one of you can create an array of.

Processing has a random number function. Use that to get a random number between 0 and the number of AudioPlayer objects you have in the array, and use that value as the index into the array.

Thanks for trying to help us PaulS but we've now tried to do an array in diffrent ways but it wont work.
Since i've posted the code where one track is working, maybe you could try to re-write it with an array for us ?
Our deadline is tomorrow morning and I can see my self sitting here in school all night !!!!
Appriciate all help so far !

Minim doesn't seem to like any of my MP3 files. It keeps giving tag errors.

Here's a start:

Define an array of songs:

int count = ??; // How many songs there will be
AudioPlayer [] songs = new AudioPlayer[count];

Create an index to define currently playing song:

int index = 0;

Populate that array:

  song[0] = minim.loadFile("track1.mp3");

You need to populate the rest of the array.

You need to define when a new song will be chosen. Whenever a new song is to be chosen, you need to stop the current songs[index] object, and define a new value for index:

index = random(0, count);

Minim doesn't seem to like any of my MP3 files. It keeps giving tag errors.

Have you put them in the right directory?

I think so. I have a data directory in the sketch's directory. I get different errors for different files, so I presume it's finding them. I had to download Minim, as I've not used it before. I only downloaded the libraries, not the full distribution.

Thanks for your reply PaulS!

I have implemented your suggestions and this is how our complete code looks like:

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import cc.arduino.*;
Minim minim;
import processing.serial.*;

Serial myPort;    // The serial port
PFont myFont;     // The display font
char inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed

int count = 2;
AudioPlayer [] songs = new AudioPlayer[count];

int index = 0;

void setup() {
  size(400, 200);
  minim = new Minim(this);

  songs[0] = minim.loadFile("track1.mp3");
  songs[1] = minim.loadFile("track2.mp3");

  // List all the available serial ports:
  println(Serial.list());


  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.buffer(3);
}

void draw() {

  while (inString == 'O' && !songs.isPlaying ()) {   // I have an arduino-code thats sends the char O if a sensor is detecting movement

    play();
  }       
  if (songs.isPlaying()) {
    // song.close();
    // minim.stop();
    // super.stop();
  }

  delay(200);
}


void play() {
  if (!songs.isPlaying()) {

    // The play() function plays the sound, as long as it is not already playing. 
    // rewind() ensures the sound starts from the beginning.
    songs.rewind(); 
    songs.play();

    songs[index].stop;

    index = random(0, count);
  }
}




void serialEvent(Serial myPort) {
  inString = myPort.readChar();
  print(inString);
}
void stop()
{
  songs.close();
  minim.stop();

  super.stop();
}

Now Processing says "Cannot invoke IsPlaying on the array type AudioPlayer[]

How can i write code that plays the arraysounds without "IsPlaying"?

Thanks again!!

  if (songs.isPlaying()) {

songs is an array of objects. The array is not playing. One of the objects (songs[index]) is.

Now the program gives us the error
"cannot convert from float to int"
in this line "index = random(0, count);"........

The random() function does return a float. You should be able to cast it to an int:

index = (int)random(0, count);

Apparently Minim doesn't like MP3s that have been through iTunes.

Finally I found a solution. Many thanks for all the great help!

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import cc.arduino.*;
import processing.serial.*;
Minim minim;

Serial myPort;    // The serial port
PFont myFont;     // The display font
char inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed

int count = 2;
AudioPlayer [] songs = new AudioPlayer[count];

int index = 0;
int index3 = 0; 

void setup() {
  size(400, 200);
  minim = new Minim(this);

  songs[0] = minim.loadFile("track1.mp3");
  songs[1] = minim.loadFile("track2.mp3");

  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.buffer(3);
}

void draw() {
  

  while (inString == 'O'&& !songs[index3].isPlaying ()) {

   play();
  }       

  if (songs[index3].isPlaying()) {
 
  }

  delay(200);
}


void play() {
  if (!songs[index3].isPlaying()) {
    float index2 = random(0, 2);
    index3= int(index2); 
    // The play() function plays the sound, as long as it is not already playing. 
    // rewind() ensures the sound starts from the beginning.
    songs[index3].rewind(); 
    songs[index3].play();

//songs[index].stop();


  }
}

void serialEvent(Serial myPort) {
  inString = myPort.readChar();
  print(inString);
  
}
void stop()
{
  for(int j=0; j<2;j++){
  songs[j].close();
  minim.stop();

  super.stop();
  }
}