Playing random music from sd card

Hello guys. I am making a interactive robot which responds to us using preloaded sounds from sd card. I have created a an app using mit app inventor and use it to send particular commands to arduino via Bluetooth which triggers a specific sound through the speaker. What i want is that.. Whenever i am not sending any voice commands to it, it should play random robotic sounds from the sd card.As soon as i give some commands the random sound should stop. A thing to be noted is that it should select random sounds from a selected group of sounds, because there are other sounds also in the sd card, which I want to play only when commanded. I am using tmrpcm library. Please give me an example code for this.

Please give me an example code for this.

After you show us the code you currently have. And, tell us the names of the files you want to randomly play.

#include <ServoTimer2.h>
ServoTimer2 myservo;

#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm music;
String voice;
const int IN1 = 7;
const int IN2 = 4;
const int IN3 = 8;
const int IN4 = 2;


const int ENA = 5;
const int ENB = 6;


void setup() {

  myservo.attach(3);
music.speakerPin=9;
Serial.begin(38400);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
music.setVolume(5);

pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
 

   const int ENA = 5;
const int ENB = 6;

}
//-----------------------------------------------------------------------//  
void loop() {
  while (Serial.available()){  //Check if there is an available byte to read
  delay(10); //Delay added to make thing stable 
  char c = Serial.read(); //Conduct a serial read
  if (c == '#') {break;} //Exit the loop when the # is detected after the word
  voice += c; //Shorthand for voice = voice + c
 

  }  


    
   analogWrite(ENA, 100);
  analogWrite(ENB, 100); 
  
  if (voice.length() > 0) {
    Serial.println(voice);

  if(voice == "play faded") 
  {
    music.play("faded.wav");
  } 
  
  if(voice == "play hello") 
  {
    music.play("hello.wav");
  } 
  
  if(voice == "play alone") 
  {
    music.play("alone.wav");
  } 
   if(voice == "what is your name") 
  {
    music.play("name.wav");
  } 
   if(voice == "who created you") 
  {
    music.play("creator.wav");
  } 
   if(voice == "which is his class") 
  {
    music.play("class.wav");
  } 
   if(voice == "who are you") 
  {
    music.play("robot.wav");
  } 
   if(voice == "play midnight") 
  {
    music.play("midnight.wav");
  }  
  if(voice == "play fat rat") 
  {
    music.play("fatrat.wav");
  } 
  if(voice == "play animals") 
  {
    music.play("animals.wav");
  } 

   if(voice == "stop music") 
  {
    music.stopPlayback();
  } 
if(voice == "pause") 
  {
    music.pause();
  } 
  if(voice == "resume") 
  {
    music.pause();
  } 
  if(voice == "45"){myservo.write(1250);
delay(100);}
  if(voice == "90"){myservo.write(1500);
  delay(100);}

   if(voice == "0"){myservo.write(1000);
  delay(100);}
   if(voice == "180"){myservo.write(2000);
 delay(100);}
  if(voice == "down") 
  {
     digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  } 

   if(voice == "front") 
  {
    digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  } 

   if(voice == "stop") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 

    if(voice == "front1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "down1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "left1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "right1") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "right") 
  {
    digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  } 
  if(voice == "left") 
  {
    digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  } 
voice="";}}

the sounds which i want to play random are not in the code. there are 16 sounds which want to randomly with a gap of 2 seconds between them.you can name sounds as randomsound1, randomsound2…etc.i am also controlling 2 dc motors and a servo motor with it. I use buttons in the app to control them and not voice.However for simplicity i just used just if(voice==“abc”); to control all speaker, dc motor and servo . i want the random sounds to play even when the dc and servo motors are working. so i think the code highlighted in bold above may cause problem.
please help me

so i think the code highlighted in bold above may cause problem.

Do YOU see any bold code?

i mean this - if(voice=="abc");

Anvay:
i mean this - if(voice=="abc");

Which line, in the code you posted, is that?

it was used many times in the code to play sounds from speaker , control dc and servo motor.#the ac written is just any text like "play faded"

Did you get any solution paul?

When there is no voice command to process you could check to see if a sound is already playing and if not choose one to play. You'll have to think about what happens when a voice command is received while one of the random sounds is playing - you can either stop the sound or wait for it to finish.

As for the 2 second delay between sounds it would be easiest to have 2 seconds of silence at the end of each sound but you could also use millis() to determine how long it has been since the last sound stopped playing.

The code below might get you started.

char *randomSounds[] = {"sound1.wav", "sound2.wav", "sound3.wav"};

//-----------------------------------------------------------------------//
void loop() {

  while (Serial.available()) { //Check if there is an available byte to read

    delay(10); //Delay added to make thing stable
    char c = Serial.read(); //Conduct a serial read
    if (c == '#') {
      break; //Exit the loop when the # is detected after the word
    }
    voice += c; //Shorthand for voice = voice + c

  }

  // if not processing a command and no sound is playing
  if( (voice.length() == 0) && (!music.isPlaying()) )
  {

    // start random sound
    music.play(randomSounds[random(sizeof(randomSounds) / sizeof(randomSounds[0]))]);

  }

Thanks. I will try it