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();
}
}
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.
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 !
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:
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.
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"?
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();
}
}