Added multiple stream types to play from playlist
This commit is contained in:
@@ -18,19 +18,28 @@ void setupAudio() {
|
||||
}
|
||||
|
||||
String nextAudio() {
|
||||
String songPath;
|
||||
String url;
|
||||
|
||||
currentPlaylistPosition++; // increment once
|
||||
songPath = getSongFromPlaylist(currentPlaylist, currentPlaylistPosition);
|
||||
if (songPath == "") { // if the end of the playlist is reached go to start
|
||||
url = getURLFromPlaylist(currentPlaylist, currentPlaylistPosition);
|
||||
|
||||
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
|
||||
nextAudio(); // recursive call
|
||||
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";
|
||||
|
||||
// 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.copyright = "";
|
||||
|
||||
return songPath;
|
||||
return url;
|
||||
}
|
||||
|
||||
String previousAudio() {
|
||||
String songPath;
|
||||
String url;
|
||||
|
||||
currentPlaylistPosition--;
|
||||
if (currentPlaylistPosition < 0) currentPlaylistPosition = 0;
|
||||
|
||||
songPath = getSongFromPlaylist(currentPlaylist, currentPlaylistPosition);
|
||||
Serial.printf("[INFO] Starting previous audio from playlist (ID: %d, Path: %s)\n", currentPlaylistPosition, songPath.c_str());
|
||||
audio.connecttoFS(SD, songPath.c_str()); // play the previous song
|
||||
url = getURLFromPlaylist(currentPlaylist, currentPlaylistPosition);
|
||||
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 = songPath;
|
||||
pbInfo.resourcePath = url;
|
||||
pbInfo.type = "local";
|
||||
|
||||
// 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.copyright = "";
|
||||
|
||||
return songPath;
|
||||
return url;
|
||||
}
|
||||
|
||||
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) {
|
||||
Serial.printf("[Audio.h] EOF_Speech %s\n", info);
|
||||
nextAudio();
|
||||
}
|
||||
void audio_info(const char *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
|
||||
Serial.printf("[Audio.h] ID3Data %s\n", info);
|
||||
|
@@ -95,7 +95,7 @@ bool createPlaylistFromDirectory(String folderpath) { // create a .m3u playlist
|
||||
return true;
|
||||
}
|
||||
|
||||
String getSongFromPlaylist(String path, int position) {
|
||||
String getURLFromPlaylist(String path, int position) {
|
||||
String song;
|
||||
File playlist;
|
||||
char currentChar;
|
||||
|
@@ -201,7 +201,7 @@ void api_v1_playlist_get() {
|
||||
bool firstRun = true;
|
||||
|
||||
while (true) {
|
||||
currentSong = getSongFromPlaylist(currentPlaylist, index);
|
||||
currentSong = getURLFromPlaylist(currentPlaylist, index);
|
||||
if (currentSong == "") {
|
||||
content += "\"";
|
||||
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
|
||||
} else {
|
||||
currentPlaylist = playlistPath;
|
||||
|
Reference in New Issue
Block a user