folder play / next song fixes

This commit is contained in:
2025-12-13 23:18:48 +01:00
parent 69bc259a6c
commit 6ecb54e5ee
4 changed files with 83 additions and 58 deletions

View File

@@ -572,6 +572,10 @@ DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String &currentGloba
void DirectoryNode::buildFlatMP3List(std::vector<std::pair<DirectoryNode *, int>> &allMP3s)
{
#ifdef DEBUG
Serial.println("Building flat mp3 list for folder");
#endif
// Pre-reserve to reduce reallocations
allMP3s.reserve(allMP3s.size() + mp3Files.size());
// Add all MP3 files from this directory
@@ -594,64 +598,56 @@ size_t DirectoryNode::getNumOfFiles() const
DirectoryNode *DirectoryNode::advanceToNextMP3(const String &currentGlobal)
{
bool useFirst = false;
Serial.println(currentGlobal.c_str());
// Build a flat list of all MP3 files in order to correctly find the next one across directories
std::vector<std::pair<DirectoryNode *, int>> allMP3s;
buildFlatMP3List(allMP3s);
if (allMP3s.empty())
{
Serial.println(F("advanceToNextMP3: No MP3s found in tree"));
currentPlaying = "";
return this;
}
int currentIndex = -1;
if (!currentGlobal.isEmpty())
{
for (size_t i = 0; i < mp3Files.size(); i++)
for (size_t i = 0; i < allMP3s.size(); i++)
{
buildFullPath(mp3Files[i], buffer, buffer_size);
if (currentGlobal == String(buffer))
DirectoryNode *node = allMP3s[i].first;
int fileIndex = allMP3s[i].second;
node->buildFullPath(node->mp3Files[fileIndex], buffer, buffer_size);
if (comparePathWithString(buffer, currentGlobal))
{
// Found the current playing MP3 file
if (i < mp3Files.size() - 1)
{
// Advance to the next MP3 file in the same directory
setCurrentPlaying(mp3Files[i + 1]);
return this;
}
useFirst = true;
// Reached the end of the MP3 files in the directory
currentIndex = (int)i;
break;
}
}
}
// We are either not playing, or we've exhausted all the MP3 files in this directory.
// Therefore, we need to recursively look in our subdirectories.
for (auto subdir : subdirectories)
// If current song found and not the last one, move to next
if (currentIndex >= 0 && currentIndex < (int)allMP3s.size() - 1)
{
if (useFirst && subdir->mp3Files.size() > 0)
{
subdir->setCurrentPlaying(subdir->mp3Files[0]);
return subdir;
}
// Have each subdirectory advance its song
for (size_t i = 0; i < subdir->mp3Files.size(); i++)
{
subdir->buildFullPath(subdir->mp3Files[i], buffer, buffer_size);
if (currentGlobal == String(buffer))
{
// Found the current playing MP3 file
if (i < subdir->mp3Files.size() - 1)
{
// Advance to the next MP3 file in the same directory
subdir->setCurrentPlaying(subdir->mp3Files[i + 1]);
return subdir;
}
else
{
useFirst = true;
}
// Reached the end of the MP3 files in the directory
break;
}
}
DirectoryNode *nextNode = allMP3s[currentIndex + 1].first;
int nextFileIndex = allMP3s[currentIndex + 1].second;
nextNode->setCurrentPlaying(nextNode->mp3Files[nextFileIndex]);
return nextNode;
}
// If not playing anything (start), play first
if (currentIndex == -1 && currentGlobal.isEmpty())
{
DirectoryNode *nextNode = allMP3s[0].first;
int nextFileIndex = allMP3s[0].second;
nextNode->setCurrentPlaying(nextNode->mp3Files[nextFileIndex]);
return nextNode;
}
// If we get here, there were no MP3 files or subdirectories left to check
// If we get here, either we are at the last song, or the current song was not found
currentPlaying = "";
Serial.println(F("no more nodes found"));
return this;