[ai] Resume to progress in s fixed

This commit is contained in:
Stefan Ostermann 2025-08-17 22:07:59 +02:00
parent b8e1263fb3
commit 72781503fa
2 changed files with 33 additions and 26 deletions

View File

@ -58,14 +58,14 @@ Audio audio;
uint volume = 7; uint volume = 7;
// Folder-play tracking: flattened list of files inside a mapped folder and current index // 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) // Used when a mapping targets a folder (play folder once or loop folder)
#include <vector> #include <vector>
static std::vector<std::pair<DirectoryNode*, int>> folderFlatList; static std::vector<std::pair<DirectoryNode *, int>> folderFlatList;
static int folderFlatIndex = -1; static int folderFlatIndex = -1;
static String folderRootPath = ""; static String folderRootPath = "";
// Pointer to the root DirectoryNode for active folder-mode playback // Pointer to the root DirectoryNode for active folder-mode playback
DirectoryNode* folderRootNode = nullptr; DirectoryNode *folderRootNode = nullptr;
AsyncWebServer server(80); AsyncWebServer server(80);
DNSServer dns; DNSServer dns;
@ -391,9 +391,7 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
} }
Serial.print("Playing by ID: "); Serial.print("Playing by ID: ");
Serial.print(id); Serial.println(id);
Serial.print(" ");
Serial.println(continueSeconds);
Serial.println(mp3File.c_str()); Serial.println(mp3File.c_str());
deactivateRFID(); deactivateRFID();
@ -405,10 +403,10 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
currentNode = nullptr; currentNode = nullptr;
return; return;
} }
if (continueSeconds > 0)
if (continueSeconds != 0)
{ {
audio.setAudioPlayPosition(continueSeconds); pendingSeekSeconds = continueSeconds;
pendingSeek = true;
} }
} }
@ -561,7 +559,6 @@ void playSongByRFID(String id)
return; return;
} }
} }
/** /**
@ -611,9 +608,10 @@ void playNextMp3()
Serial.print("Advancing to "); Serial.print("Advancing to ");
String mp3File = currentNode->getCurrentPlayingFilePath(); String mp3File = currentNode->getCurrentPlayingFilePath();
//FIXME crash here if last song. // FIXME crash here if last song.
if (mp3File.isEmpty()) { if (mp3File.isEmpty())
{
currentNode = rootNode.findFirstDirectoryWithMP3s(); currentNode = rootNode.findFirstDirectoryWithMP3s();
return; return;
} }
@ -739,7 +737,6 @@ String getState()
output.reserve(512); // Pre-allocate string buffer output.reserve(512); // Pre-allocate string buffer
output.clear(); output.clear();
jsonState.clear(); // Clear previous data jsonState.clear(); // Clear previous data
jsonState["playing"] = audio.isRunning(); jsonState["playing"] = audio.isRunning();
@ -761,7 +758,6 @@ String getState()
jsonState["uid"] = lastUid; jsonState["uid"] = lastUid;
jsonState["heap"] = free_heap; jsonState["heap"] = free_heap;
serializeJson(jsonState, output); serializeJson(jsonState, output);
return output; return output;
@ -1265,8 +1261,7 @@ void setup()
// Fallback: serve minimal error if file not found // Fallback: serve minimal error if file not found
request->send(404, "text/plain", "ERROR: /system/index.html 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) server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request)
{ {
@ -1284,8 +1279,7 @@ void setup()
// Fallback: serve minimal CSS if file not found // Fallback: serve minimal CSS if file not found
request->send(404, "text/plain", "ERROR: /system/style.css 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) server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
{ {
@ -1302,8 +1296,7 @@ void setup()
// Fallback: serve minimal JS if file not found // Fallback: serve minimal JS if file not found
request->send(404, "text/plain", "ERROR: /system/script.js not found!"); request->send(404, "text/plain", "ERROR: /system/script.js not found!");
} }
webrequestActive = false; webrequestActive = false; });
});
server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request) server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request)
{ {
@ -1451,6 +1444,16 @@ void loop()
{ {
currentNode->setSecondsPlayed(audio.getAudioCurrentTime()); 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) else if (asyncStart)
{ {
@ -1463,6 +1466,7 @@ void loop()
continuePlaying = false; continuePlaying = false;
startupSoundPlayed = true; startupSoundPlayed = true;
playSongById(currentSongId, currentSongSeconds); playSongById(currentSongId, currentSongSeconds);
currentNode->setSecondsPlayed(currentSongSeconds);
} }
else if (!startupSoundPlayed) else if (!startupSoundPlayed)
{ {

View File

@ -99,4 +99,7 @@ std::map<String, MappingEntry> rfid_map;
// track of the folder root node so EOF handling can advance only inside that folder. // track of the folder root node so EOF handling can advance only inside that folder.
bool folderModeActive = false; bool folderModeActive = false;
bool pendingSeek = false;
uint32_t pendingSeekSeconds = 0;
#endif #endif