Hi guys,
I have a function getFile for get file MP3 from micro sd.
My problem is when calling the function and run on while (file.available()) and same time together any request from client, server.handleClient() cannot running well (I think still loop on while (file.available())).
server.handleClient() will be running after finished loop while (file.available()) .
How do I run both codes at the same time?
void getFile(const String& path, const String& contentType) {
File file = SD.open(path);
if (!file) {
String json = "{\"message\": \"Hello, ESP32!\", \"status\": \"success\"}";
server.send(200, "application/json", json);
return;
}
size_t fileSize = file.size();
server.setContentLength(fileSize);
server.sendHeader("Content-Type", contentType);
server.sendHeader("Cache-Control", "public, jws_AR=31536000"); // Cache for 1 years
server.sendHeader("Connection", "close");
server.send(200, contentType, "");
const size_t bufferSize = 512; // Size of each chunk
uint8_t buffer[bufferSize];
while (file.available()) {
// server.handleClient();
if (!_StopMp3) {
size_t bytesRead = file.read(buffer, bufferSize);
// Serial.print("Bytes read: ");
// Serial.println(bytesRead);
server.client().write(buffer, bytesRead);
}
if (_StopMp3) {
_StopMp3 = false;
Serial.println("STOP PLAY");
break;
}
}
file.close();
}
