please format code with </> button * homework policy * asking questions
Hi
I am trying to randomly play audio files on a pi zero (yes you can play back audio files on a pi zero, as demonstrated here ) and do a few other things. It’s for an art installation that is located in multiple places all at once (hence I need to keep the micro controller price as low as possible).
I’ve applied @kfrajer 's code (via @ charmypatel) shuffle song code and it works beautifully (thanks for that piece of code btw). That is … as long as I play it on my laptop… On the pi, after three files (these are short mp3, under 3 min long) I start getting the:
Blockquote
Null pointer exception
=== JavaSound Mhinim Error ===
=== Unable to return a SourceDataLine: unsuported format - PCM_SIGNED 8000.0 Hz, mono, 2 bytes/frame, little-endian
=== Minim Error ===
=== Couldn’t load the file 1.mp3 error message.
However if I simply play back an audio file, even a large .wav file I don’t get that error message. Hence I am thinking that the pi gets overloaded when I shuffle the files?
I was wondering if there was another way of loading the files, or perhaps having multiple players and then randomizing (shuffling) the players instead of the files? I am not too sure how to go about this. I have tried using arrays to randomize the players but that does not work as players are not objects.
Anybody has any idea? I noticed @bluemoon proposed somewhat of a fix to a similar problem, but nobody confirmed whether it worked or not, hence I am thinking it probably did not … ?
I’m pasting my code down here so you can all see (I did not modify @kfrajer’s code, I just inserted it into a series of if statement; as you will see):
import processing.io.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
boolean playeurInit = false;// que leplayer n'est pas lancé
boolean stop = true;
StringList tableau;
int current=0;
// GPIO numbers refer to different phyiscal pins on various boards
// On the Raspberry Pi GPIO 4 is physical pin 7 on the header
//Thanks to kfrajer (via charmypatel) for the shuffle code
void setup() {
// INPUT_PULLUP enables the built-in pull-up resistor for this pin
// left alone, the pin will read as HIGH
// connected to ground (via e.g. a button or switch) it will read LOW
GPIO.pinMode(05, GPIO.INPUT_PULLUP);
size(100, 100);
minim = new Minim(this);
//ADD sound files to your list
tableau=new StringList();
tableau.append("1.mp3");
tableau.append("2.mp3");
tableau.append("3.mp3");
shufflePlayList();
String son = tableau.get(current++);
println(son);
player = minim.loadFile(son);
player.pause();
}
void draw()
{
if (GPIO.digitalRead(05) == GPIO.HIGH)
{
// sensor picks up movement
fill(255);
if (!stop) {
if (player.isPlaying() == false) {
//CHECK we haven't exhausted the current shuffled(random) play list
//If we have played all the songs, then re-shuffle the list
if (current==tableau.size()-1) {
shufflePlayList();
}
String son = tableau.get(current++);
player = minim.loadFile(son);
player.play();
playeurInit = true;
}
}
}
}
if (GPIO.digitalRead(05) == GPIO.LOW)
{
// sensor picks up no movement
fill(204);
//player.pause();
{
fill(204);
}
}
stroke(255);
ellipse(width/2, height/2, width*0.75, height*0.75);
}
void mousePressed()
{
if (stop == true) {
stop = false;
} else {
stop = true;
if (player.isPlaying() == true ) {
player.pause();
}
}
}
void stop()
{
player.close();
minim.stop();
super.stop();
}
void shufflePlayList() {
current=0;
tableau.shuffle();
}