How to write mp3 file to SPIFFS?
Posted: Sat Apr 17, 2021 1:44 pm
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
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();