earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
2.02k stars 431 forks source link

Check for File presence in SPIFFS #161

Closed dagnall53 closed 4 years ago

dagnall53 commented 5 years ago

Earle, I am sure this is trivial, but I am trying to put in checks that a File actually exists before allowing my sound code to run. I tried something like this

if (AudioFileSourceSPIFFS.open(wavfilename)){
           file[Channel]= new AudioFileSourceSPIFFS(wavfilename);}
else {DebugSprintfMsgSend( sprintf ( DebugMsg,"SFX No such file name"));     }

But I get "expected primary-expression before '.' token"

Is there an easy way to use the file open Boolean that seems to be in your AudioFileSourceSpiffs.cpp (bool AudioFileSourceSPIFFS::open(const char *filename) ?

Reason for doing this is that the code seems to accept non present SPIFFS filenames, but then does not detect end of file, so "plays" non stop.. (thus stopping me starting a new file sound play). I'm trying to update my Rocrail sound stuff to ESP32 and wanted to put in some checks to make it more bulletproof.. Huge thanks Dagnall

dagnall53 commented 5 years ago

I am now using this code in my esp8266 in case anyone want to copy or improve..BUT it odes not work with ESP32 ! ` bool FileExists(const char *wavfilename){ File TestObj; if (!(TestObj= SPIFFS.open(wavfilename,"r"))){

ifdef _SERIAL_Audio_DEBUG

    DebugSprintfMsgSend( sprintf ( DebugMsg, " SFX File not found %s",wavfilename)); // sends me a debug message
#endif
    malloc(TestObj); 
    return false; 
    }

malloc(TestObj); return true; }`

dagnall53 commented 5 years ago

Opened again as The code above only works for ESP8266 NOT esp32.. (although my esp32 does seem to be stable even if I open a non existent file..!)

earlephilhower commented 4 years ago
if (AudioFileSourceSPIFFS.open(wavfilename)){
           file[Channel]= new AudioFileSourceSPIFFS(wavfilename);}
else {DebugSprintfMsgSend( sprintf ( DebugMsg,"SFX No such file name"));     }

But I get "expected primary-expression before '.' token"

You've got a C++ syntax problem. You need to make an object of the class, then use that variable to call open and check the return value:

AudioFileSourceSPIFFS f;
if (!f.open(wavfile)) ....