arduino yun + usb sound card + random sound

Hi

i have this project in which i have to use a yun and a usb sound card to randomly play mp3's stored in my sd card. i followed this tutorial and it works

http://dev.mikamai.com/post/69775973742/arduino-yún-with-sound-the-supereasy-way

however i need help in creating a code that with the one push of a button will play a mp3 sound from the sd card randomly. i have 10 or more sound clips which i want to play randomly and need help with this

#include <Process.h>

const int buttonPin = 2;
const int ledPin =  13;

int buttonState = 0;
Process p;

void setup() {
  Bridge.begin();
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  Serial.begin(57600);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);  
    p.runShellCommand("madplay /mnt/sda1/worldOfTomorrow.mp3");
    while(p.running());  
    Serial.println("it works!");
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

above is the code which plays the one mp3 file

i was wondering if there is a way in which the yun can read the number of files inside the sd card first, store them in a array. then read all the file names inside the sd and then produce the sounds

if anyone can help me that would be great

thank you

Use the FileIO : http://www.arduino.cc/en/Reference/YunFileIOConstructor
And 'walk' through the files with rewindDirectory() and openNextFile().
It should work in the same way as the normal SD library : SD - Arduino Reference

Did you know that it also possible to play online radio with wget and madplay ?

wget -O - http://wgltradio.ilstu.edu:8000/wgltblues.mp3 | madplay -o /dev/dsp -

Don't forget the '-' at the end

Which USB sound card do you use ? I'm looking for a better quality than my USB sound card from Ebay.

Koepel:
Use the FileIO : http://www.arduino.cc/en/Reference/YunFileIOConstructor
And 'walk' through the files with rewindDirectory() and openNextFile().
It should work in the same way as the normal SD library : SD - Arduino Reference

Did you know that it also possible to play online radio with wget and madplay ?

wget -O - http://wgltradio.ilstu.edu:8000/wgltblues.mp3 | madplay -o /dev/dsp -

Don't forget the '-' at the end

Which USB sound card do you use ? I'm looking for a better quality than my USB sound card from Ebay.

i was wondering if i can access the number of files in my sd card from the command shell, i think i have it figured out but im having a bit of trouble

the codes below works in the terminal and gives me the number 4

cd /mnt/sda1
ls | wc - l

is there a way in getting these commands to work in the command shell in the sketch

i have tried this but i haven't got it to work right now there are 4 files in my sd card root

d.runShellCommand("ls | wc -l | cd /mnt/sda1"); 
  result = d.parseInt();
Serial.println(result);

i just keep getting zero

i got a really cheap usb sound card not good sound quality but i can hear it a bit

thank you

You don't have to quote my post, what I wrote is already there :wink:

I use "/mnt/sd" and let OpenWRT make that into "/mnt/sda1".
I'm using "SerialUSB", that seems to be the new preferred name.
Because my folder with mp3 files are part of a website, I have it located a few folders deep.

You want to redirect the output of the wordcount into a folder ? It is simpler as you can see in this example:

  // Number of files in the mp3 folder
  SerialUSB.print(F( "Number of files in mp3 folder : "));
  yunProcess.runShellCommand(F( "ls /mnt/sd/arduino/www/mp3 | wc -l"));
  int result = yunProcess.parseInt();
  SerialUSB.println( result);
  // Remove any remaining characters
  while(yunProcess.available() > 0) 
    yunProcess.read();
  SerialUSB.println();

For the FileIO I have included FileIO.h and have called FileSystem.begin() in setup().

  // List the files in the mp3 folder
  int found = 0;
  File folder = FileSystem.open( "/mnt/sd/arduino/www/mp3");
  folder.rewindDirectory();

  while( File entry = folder.openNextFile())      // stops if openNextFile returns zero
  {
    found++;
    SerialUSB.println( entry.name());
    if( entry.isDirectory())
      found--;
  }
  
  SerialUSB.print(F( "Number of files found: "));
  SerialUSB.println( found);
  SerialUSB.println();