From 72781503fad4b68afbbb0d5d5cf87b0a7697e617 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Sun, 17 Aug 2025 22:07:59 +0200 Subject: [PATCH] [ai] Resume to progress in s fixed --- src/main.cpp | 56 ++++++++++++++++++++++++++++------------------------ src/main.h | 3 +++ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 810f73c..4e359a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,14 +58,14 @@ Audio audio; uint volume = 7; - // Folder-play tracking: flattened list of files inside a mapped folder and current index - // Used when a mapping targets a folder (play folder once or loop folder) - #include - static std::vector> folderFlatList; - static int folderFlatIndex = -1; - static String folderRootPath = ""; - // Pointer to the root DirectoryNode for active folder-mode playback - DirectoryNode* folderRootNode = nullptr; +// Folder-play tracking: flattened list of files inside a mapped folder and current index +// Used when a mapping targets a folder (play folder once or loop folder) +#include +static std::vector> folderFlatList; +static int folderFlatIndex = -1; +static String folderRootPath = ""; +// Pointer to the root DirectoryNode for active folder-mode playback +DirectoryNode *folderRootNode = nullptr; AsyncWebServer server(80); DNSServer dns; @@ -391,9 +391,7 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0) } Serial.print("Playing by ID: "); - Serial.print(id); - Serial.print(" "); - Serial.println(continueSeconds); + Serial.println(id); Serial.println(mp3File.c_str()); deactivateRFID(); @@ -405,10 +403,10 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0) currentNode = nullptr; return; } - - if (continueSeconds != 0) + if (continueSeconds > 0) { - audio.setAudioPlayPosition(continueSeconds); + pendingSeekSeconds = continueSeconds; + pendingSeek = true; } } @@ -561,7 +559,6 @@ void playSongByRFID(String id) return; } - } /** @@ -611,9 +608,10 @@ void playNextMp3() Serial.print("Advancing to "); String mp3File = currentNode->getCurrentPlayingFilePath(); - //FIXME crash here if last song. - if (mp3File.isEmpty()) { - + // FIXME crash here if last song. + if (mp3File.isEmpty()) + { + currentNode = rootNode.findFirstDirectoryWithMP3s(); return; } @@ -739,7 +737,6 @@ String getState() output.reserve(512); // Pre-allocate string buffer output.clear(); - jsonState.clear(); // Clear previous data jsonState["playing"] = audio.isRunning(); @@ -761,7 +758,6 @@ String getState() jsonState["uid"] = lastUid; jsonState["heap"] = free_heap; - serializeJson(jsonState, output); return output; @@ -1265,8 +1261,7 @@ void setup() // Fallback: serve minimal error if file not found request->send(404, "text/plain", "ERROR: /system/index.html not found!"); } - webrequestActive = false; - }); + webrequestActive = false; }); server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) { @@ -1284,8 +1279,7 @@ void setup() // Fallback: serve minimal CSS if file not found request->send(404, "text/plain", "ERROR: /system/style.css not found!"); } - webrequestActive = false; - }); + webrequestActive = false; }); server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request) { @@ -1302,8 +1296,7 @@ void setup() // Fallback: serve minimal JS if file not found request->send(404, "text/plain", "ERROR: /system/script.js not found!"); } - webrequestActive = false; - }); + webrequestActive = false; }); server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request) { @@ -1451,6 +1444,16 @@ void loop() { currentNode->setSecondsPlayed(audio.getAudioCurrentTime()); } + // Apply pending seek once decoder is ready (after header parsed and bitrate known) + if (pendingSeek && audio.getBitRate(true) > 0 && audio.getAudioFileDuration() > 0) + { + audio.setAudioPlayPosition(pendingSeekSeconds); + if (currentNode != nullptr) + { + currentNode->setSecondsPlayed(pendingSeekSeconds); + } + pendingSeek = false; + } } else if (asyncStart) { @@ -1463,6 +1466,7 @@ void loop() continuePlaying = false; startupSoundPlayed = true; playSongById(currentSongId, currentSongSeconds); + currentNode->setSecondsPlayed(currentSongSeconds); } else if (!startupSoundPlayed) { diff --git a/src/main.h b/src/main.h index d43312e..55a66f1 100644 --- a/src/main.h +++ b/src/main.h @@ -99,4 +99,7 @@ std::map rfid_map; // track of the folder root node so EOF handling can advance only inside that folder. bool folderModeActive = false; +bool pendingSeek = false; +uint32_t pendingSeekSeconds = 0; + #endif