playlist
This commit is contained in:
parent
220f959e51
commit
3a19563278
@ -2,11 +2,14 @@
|
|||||||
|
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "playlist.h"
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
playerSetup();
|
playerSetup();
|
||||||
|
playlistClear();
|
||||||
|
playlistAdd("http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
#include "playlist.h"
|
||||||
|
|
||||||
#define DELAY_MS_ADD 250UL
|
#define DELAY_MS_ADD 250UL
|
||||||
#define DELAY_MS_MAX 5000UL
|
#define DELAY_MS_MAX 5000UL
|
||||||
@ -23,14 +24,10 @@ void playerLoop() {
|
|||||||
if (errorMs > 0 && millis() - errorMs < delayMs) {
|
if (errorMs > 0 && millis() - errorMs < delayMs) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
playerNext();
|
const auto url = playlistNextOrRepeatOneIfEnabled();
|
||||||
}
|
if (url.isEmpty()) {
|
||||||
|
return;
|
||||||
void playerNext() {
|
}
|
||||||
playerPlay("http://liveradio.sr.de/sr/sr1/mp3/128/stream.mp3");
|
|
||||||
}
|
|
||||||
|
|
||||||
void playerPlay(const String &url) {
|
|
||||||
if (audioPlay(url)) {
|
if (audioPlay(url)) {
|
||||||
errorMs = 0;
|
errorMs = 0;
|
||||||
delayMs = 0;
|
delayMs = 0;
|
||||||
|
|||||||
@ -1,12 +1,6 @@
|
|||||||
#ifndef PLAYER_H
|
#ifndef PLAYER_H
|
||||||
#define PLAYER_H
|
#define PLAYER_H
|
||||||
|
|
||||||
#include <WString.h>
|
|
||||||
|
|
||||||
void playerNext();
|
|
||||||
|
|
||||||
void playerPlay(const String &url);
|
|
||||||
|
|
||||||
void playerSetup();
|
void playerSetup();
|
||||||
|
|
||||||
void playerLoop();
|
void playerLoop();
|
||||||
|
|||||||
91
src/playlist.cpp
Normal file
91
src/playlist.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#include "playlist.h"
|
||||||
|
|
||||||
|
#include <SD.h>
|
||||||
|
|
||||||
|
#include "AudioTools/AudioLibs/I2SCodecStream.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
bool playlistRepeatOne = false;
|
||||||
|
|
||||||
|
bool playlistRepeatAll = false;
|
||||||
|
|
||||||
|
bool playlistRandom = false;
|
||||||
|
|
||||||
|
String playlistTitle = "";
|
||||||
|
|
||||||
|
size_t playlistIndex = 0;
|
||||||
|
|
||||||
|
std::vector<String> playlistEntries;
|
||||||
|
|
||||||
|
void playlistClear() {
|
||||||
|
playlistTitle = "";
|
||||||
|
playlistIndex = 0;
|
||||||
|
playlistEntries.clear();
|
||||||
|
std::vector<String>().swap(playlistEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
void playlistAdd(String url) {
|
||||||
|
url.trim();
|
||||||
|
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("sd://") || url.startsWith("snap://")) {
|
||||||
|
playlistEntries.push_back(url);
|
||||||
|
} else if (url.startsWith("title://")) {
|
||||||
|
playlistTitle = url.substring(8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void playlistLoad(const String &path) {
|
||||||
|
playlistClear();
|
||||||
|
Serial.println("[PLAYLIST] Loading playlist: " + path);
|
||||||
|
if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) {
|
||||||
|
Serial.println("[PLAYLIST] Failed to initialize SD card.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!SD.exists(path)) {
|
||||||
|
Serial.println("[PLAYLIST] File not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto file = SD.open(path, FILE_READ);
|
||||||
|
if (!file) {
|
||||||
|
Serial.println("[PLAYLIST] Failed to open file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (file.available() > 0) {
|
||||||
|
String entry = file.readStringUntil('\n');
|
||||||
|
playlistAdd(entry);
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlistSet(const size_t index) {
|
||||||
|
const auto size = playlistEntries.size();
|
||||||
|
if (size == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
playlistIndex = (index % size + size) % size;
|
||||||
|
return playlistEntries[playlistIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlistCurrent() {
|
||||||
|
return playlistSet(playlistIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlistNext() {
|
||||||
|
if (playlistIndex >= playlistEntries.size() - 1 && !playlistRepeatAll) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return playlistSet(playlistIndex + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlistNextOrRepeatOneIfEnabled() {
|
||||||
|
if (playlistRepeatOne) {
|
||||||
|
return playlistCurrent();
|
||||||
|
}
|
||||||
|
return playlistNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlistPrevious() {
|
||||||
|
if (playlistIndex <= 0 && !playlistRepeatAll) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return playlistSet(playlistIndex - 1);
|
||||||
|
}
|
||||||
20
src/playlist.h
Normal file
20
src/playlist.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef PLAYLIST_H
|
||||||
|
#define PLAYLIST_H
|
||||||
|
|
||||||
|
#include <WString.h>
|
||||||
|
|
||||||
|
void playlistClear();
|
||||||
|
|
||||||
|
void playlistAdd(String url);
|
||||||
|
|
||||||
|
void playlistLoad(const String &path);
|
||||||
|
|
||||||
|
String playlistCurrent();
|
||||||
|
|
||||||
|
String playlistNext();
|
||||||
|
|
||||||
|
String playlistNextOrRepeatOneIfEnabled();
|
||||||
|
|
||||||
|
String playlistPrevious();
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user