TMRpcm.play doesnt work without delay

i try to play a wav file. but my program doesn't work without delay function. without it there is no sound. i want to use it in interrupt so i need to delate it. how can i do it? thank you


#include <SD.h>               // need to include the SD library
#define SD_ChipSelectPin 10   //pin10 for Arduino Pro Mini
#include <TMRpcm.h>           //  Lib to play wav file
#include <SPI.h>

TMRpcm tmrpcm;   // create an object for use in this sketch


void setup(){
  tmrpcm.speakerPin = 9; //pin 9 for Aduino Pro Mini , any pin with PWM fonction will work too
  Serial.begin(9600);

  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    Serial.println("SD fail");  
    return;   // don't do anything more if not
  }
  else{   
    Serial.println("SD ok");   
  }
  
  tmrpcm.volume(2); //default volume to 2 
}

void loop(){
 tmrpcm.play("music.wav");
delay(4000);

  
}

How often do you want to start playing the same file? Several thousand times per second? That is what you have coded.

I get ur point. Then how can I say ardunio , don't play same file untill it fineshed?

I guess I will have to find the documentation for you. Are there any functions listed that might help?

TMRpcm audio;
audio.play("filename");    plays a file
audio.play("filename",30); plays a file starting at 30 seconds into the track
audio.play(F("filename")); plays a file storing the string in program memory instead of RAM
audio.speakerPin = 11;     set to 5,6,11 or 46 for Mega, 9 for Uno, Nano, etc.
audio.disable();           disables the timer on output pin and stops the music
audio.stopPlayback();      stops the music, but leaves the timer running
audio.isPlaying();         returns 1 if music playing, 0 if not
audio.pause();             pauses/unpauses playback
audio.quality(1);          Set 1 for 2x oversampling
audio.volume(0);           1(up) or 0(down) to control volume
audio.setVolume(0);        0 to 7. Set volume level
audio.loop(1);             0 or 1. Can be changed during playback for full control of looping. 

i already find this document but still couldn't solve this. any recomendation?

What about using this one?

so i need check always is audio playing . maybe in while loop? right?

thank you

If this was my code, I would add a loop to see if the music was playing right after you start it playing, and then add a loop to check if it has stopped when the music is done. Two loops!

1 Like

good advice. thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.