-->
Page 1 of 1

How to write mp3 file to SPIFFS?

PostPosted: Sat Apr 17, 2021 1:44 pm
by playground
How to write the response from “https://translate.google.com/translate_tts?ie=UTF-8&tl=en_US&client=tw-ob&q=hello%20world” to SPIFFS as mp3 file and be able to play it back. Is there an example how to save the mp3 content to SPIFFS?

I have tried this but audio is not playing

Code: Select all http.begin(tts.c_str());
  // Send HTTP GET request
  int httpResponseCode = http.GET();
  if (httpResponseCode>0) {
     Serial.print("HTTP Response code: ");
     Serial.println(httpResponseCode);
     int len = http.getSize();
     uint8_t buff[len] = {0};
     
     WiFiClient *stream = http.getStreamPtr();
   
     while(http.connected() && (len > 0 || len == -1)) {
        size_t size = stream->available();

        if(size) {
            int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
           
         }
        delay(1);
     }
     File file = SPIFFS.open("/helloworld.mp3", "w");
     file.write(buff, len);
     file.close(); 
   }
   else {
     Serial.print("Error code: ");
     Serial.println(httpResponseCode);
   }
   http.end();

Re: How to write mp3 file to SPIFFS?

PostPosted: Sat Apr 24, 2021 1:03 pm
by playground
Upon further investigation. I realized the responses I got from HTTPClient & WiFiClientSecure are different from the ones I got from Postman. I have posted https://www.esp32.com/viewtopic.php?f=19&t=20565&p=75332#p75332