Added multiple stream types to play from playlist

This commit is contained in:
2023-12-10 16:45:17 +01:00
parent cd62f273c1
commit d06f2da73d
3 changed files with 38 additions and 16 deletions

View File

@@ -18,19 +18,28 @@ void setupAudio() {
} }
String nextAudio() { String nextAudio() {
String songPath; String url;
currentPlaylistPosition++; // increment once currentPlaylistPosition++; // increment once
songPath = getSongFromPlaylist(currentPlaylist, currentPlaylistPosition); url = getURLFromPlaylist(currentPlaylist, currentPlaylistPosition);
if (songPath == "") { // if the end of the playlist is reached go to start
if (url == "") { // if the end of the playlist is reached go to start
currentPlaylistPosition = -1; // the next time "nextAudio()" is called it will increment to 0 currentPlaylistPosition = -1; // the next time "nextAudio()" is called it will increment to 0
nextAudio(); // recursive call nextAudio(); // recursive call
return ""; // exit return ""; // exit
} }
Serial.printf("[INFO] Starting next audio from playlist (ID: %d, Path: %s)\n", currentPlaylistPosition, songPath.c_str());
audio.connecttoFS(SD, songPath.c_str()); // play the next song
pbInfo.resourcePath = songPath; if (url.startsWith("http")) {
Serial.printf("[INFO] Starting stream from playlist (ID: %d, URL: %s)\n", currentPlaylistPosition, url.c_str());
audio.connecttohost(url.c_str());
} else if (url.startsWith("/")) {
Serial.printf("[INFO] Starting audio from playlist (ID: %d, Path: %s)\n", currentPlaylistPosition, url.c_str());
audio.connecttoFS(SD, url.c_str()); // play the next song
} else if (url.startsWith("speech://")) { // format: speech://CC@TEXT where CC is the country code, e.g. en or de
Serial.printf("[INFO] Starting speech from playlist (ID: %d, Language: %s, Text: %s)\n", currentPlaylistPosition, url.substring(9, 11).c_str(), url.substring(12).c_str());
audio.connecttospeech(url.substring(12).c_str(), url.substring(9, 11).c_str());
}
pbInfo.resourcePath = url;
pbInfo.type = "local"; pbInfo.type = "local";
// clear everything (if a file hadn't got id3 tags so that there won't keep the old names) // clear everything (if a file hadn't got id3 tags so that there won't keep the old names)
@@ -43,20 +52,28 @@ String nextAudio() {
pbInfo.genre = ""; pbInfo.genre = "";
pbInfo.copyright = ""; pbInfo.copyright = "";
return songPath; return url;
} }
String previousAudio() { String previousAudio() {
String songPath; String url;
currentPlaylistPosition--; currentPlaylistPosition--;
if (currentPlaylistPosition < 0) currentPlaylistPosition = 0; if (currentPlaylistPosition < 0) currentPlaylistPosition = 0;
songPath = getSongFromPlaylist(currentPlaylist, currentPlaylistPosition); url = getURLFromPlaylist(currentPlaylist, currentPlaylistPosition);
Serial.printf("[INFO] Starting previous audio from playlist (ID: %d, Path: %s)\n", currentPlaylistPosition, songPath.c_str()); if (url.startsWith("http")) {
audio.connecttoFS(SD, songPath.c_str()); // play the previous song Serial.printf("[INFO] Starting stream from playlist (ID: %d, URL: %s)\n", currentPlaylistPosition, url.c_str());
audio.connecttohost(url.c_str());
} else if (url.startsWith("/")) {
Serial.printf("[INFO] Starting audio from playlist (ID: %d, Path: %s)\n", currentPlaylistPosition, url.c_str());
audio.connecttoFS(SD, url.c_str()); // play the next song
} else if (url.startsWith("speech://")) { // format: speech://CC@TEXT where CC is the country code, e.g. en or de
Serial.printf("[INFO] Starting speech from playlist (ID: %d, Language: %s, Text: %s)\n", currentPlaylistPosition, url.substring(9, 11).c_str(), url.substring(12).c_str());
audio.connecttospeech(url.substring(12).c_str(), url.substring(9, 11).c_str());
}
pbInfo.resourcePath = songPath; pbInfo.resourcePath = url;
pbInfo.type = "local"; pbInfo.type = "local";
// clear everything (if a file hadn't got id3 tags so that there won't keep the old names) // clear everything (if a file hadn't got id3 tags so that there won't keep the old names)
@@ -69,7 +86,7 @@ String previousAudio() {
pbInfo.genre = ""; pbInfo.genre = "";
pbInfo.copyright = ""; pbInfo.copyright = "";
return songPath; return url;
} }
void switchPlaybackIfButtonClicked() { void switchPlaybackIfButtonClicked() {
@@ -121,9 +138,14 @@ void audio_eof_mp3(const char *info) { // MUST HAVE THIS NAME (audio_eof_mp3) b
} }
void audio_eof_speech(const char *info) { void audio_eof_speech(const char *info) {
Serial.printf("[Audio.h] EOF_Speech %s\n", info); Serial.printf("[Audio.h] EOF_Speech %s\n", info);
nextAudio();
} }
void audio_info(const char *info) { void audio_info(const char *info) {
Serial.printf("[Audio.h] Info %s\n", info); Serial.printf("[Audio.h] Info %s\n", info);
if(String(info).startsWith("End of webstream")) {
nextAudio();
}
} }
void audio_id3data(const char *info) { // id3 metadata void audio_id3data(const char *info) { // id3 metadata
Serial.printf("[Audio.h] ID3Data %s\n", info); Serial.printf("[Audio.h] ID3Data %s\n", info);

View File

@@ -95,7 +95,7 @@ bool createPlaylistFromDirectory(String folderpath) { // create a .m3u playlist
return true; return true;
} }
String getSongFromPlaylist(String path, int position) { String getURLFromPlaylist(String path, int position) {
String song; String song;
File playlist; File playlist;
char currentChar; char currentChar;

View File

@@ -201,7 +201,7 @@ void api_v1_playlist_get() {
bool firstRun = true; bool firstRun = true;
while (true) { while (true) {
currentSong = getSongFromPlaylist(currentPlaylist, index); currentSong = getURLFromPlaylist(currentPlaylist, index);
if (currentSong == "") { if (currentSong == "") {
content += "\""; content += "\"";
break; break;
@@ -254,7 +254,7 @@ void api_v1_playlist_play() {
} }
} }
if (getSongFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent if (getURLFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent
api_server.send(200, "application/json", generate_api_json(false)); // send no success info api_server.send(200, "application/json", generate_api_json(false)); // send no success info
} else { } else {
currentPlaylist = playlistPath; currentPlaylist = playlistPath;