fixed previous

This commit is contained in:
2025-07-07 21:28:49 +02:00
parent 29316ffd13
commit 54e3100867
3 changed files with 162 additions and 44 deletions

View File

@@ -262,39 +262,103 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *currentGlobal)
*/
DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds)
{
if (secondsPlayed > thresholdSeconds || currentPlaying == nullptr)
// Safety check for null pointer
if (currentPlaying == nullptr)
{
// Restart the current song if it's been playing for more than thresholdSeconds
// Or if there is no current song (at the start of the list)
Serial.println("goToPreviousMP3: currentPlaying is null");
return nullptr;
}
// If we've been playing for more than threshold seconds, restart current song
if (secondsPlayed > thresholdSeconds)
{
Serial.println("goToPreviousMP3: Restarting current song (played > threshold)");
return this;
}
else
{
// Find the previous song
for (size_t i = 0; i < mp3Files.size(); i++)
{
if (currentPlaying != nullptr && *currentPlaying == mp3Files[i] && i > 0)
{
// Move to the previous song
setCurrentPlaying(&mp3Files[i - 1]);
return this;
}
}
// If the first song in the directory or no song was playing, move to the previous directory, if any
for (auto subdir : subdirectories)
// Find the current song index in this directory
int currentIndex = -1;
for (size_t i = 0; i < mp3Files.size(); i++)
{
if (*currentPlaying == mp3Files[i])
{
DirectoryNode *previousNode = subdir->goToPreviousMP3(thresholdSeconds);
if (previousNode != nullptr)
{
return previousNode;
}
currentIndex = i;
break;
}
}
// No previous song available
// If current song found and not the first song, move to previous
if (currentIndex > 0)
{
Serial.print("goToPreviousMP3: Moving to previous song in same directory: ");
Serial.println(mp3Files[currentIndex - 1]);
setCurrentPlaying(&mp3Files[currentIndex - 1]);
return this;
}
// If we're at the first song or song not found in current directory,
// we need to find the previous song globally
Serial.println("goToPreviousMP3: At first song or song not found, looking for previous globally");
return nullptr; // Let the caller handle global previous logic
}
DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGlobal)
{
if (currentGlobal == nullptr)
{
Serial.println("findPreviousMP3Globally: currentGlobal is null");
return nullptr;
}
// Build a flat list of all MP3 files in order
std::vector<std::pair<DirectoryNode*, int>> allMP3s;
buildFlatMP3List(allMP3s);
// Find current song in the flat list
int currentGlobalIndex = -1;
for (size_t i = 0; i < allMP3s.size(); i++)
{
DirectoryNode* node = allMP3s[i].first;
int fileIndex = allMP3s[i].second;
if (node->mp3Files[fileIndex] == *currentGlobal)
{
currentGlobalIndex = i;
break;
}
}
// If current song found and not the first globally, move to previous
if (currentGlobalIndex > 0)
{
DirectoryNode* prevNode = allMP3s[currentGlobalIndex - 1].first;
int prevFileIndex = allMP3s[currentGlobalIndex - 1].second;
Serial.print("findPreviousMP3Globally: Moving to previous song globally: ");
Serial.println(prevNode->mp3Files[prevFileIndex]);
prevNode->setCurrentPlaying(&prevNode->mp3Files[prevFileIndex]);
return prevNode;
}
Serial.println("findPreviousMP3Globally: No previous song found globally");
return nullptr;
}
void DirectoryNode::buildFlatMP3List(std::vector<std::pair<DirectoryNode*, int>>& allMP3s)
{
// Add all MP3 files from this directory
for (size_t i = 0; i < mp3Files.size(); i++)
{
allMP3s.push_back(std::make_pair(this, i));
}
// Recursively add MP3 files from subdirectories
for (DirectoryNode* subdir : subdirectories)
{
subdir->buildFlatMP3List(allMP3s);
}
}
DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
{
bool useFirst = false;