From 35c8ec13eecb84141508c98b24bcec6d5fa88541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 5 Jun 2025 14:40:00 +0200 Subject: [PATCH] audioPlayInit (including isWifiConnected) --- src/audio.cpp | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index f2eddbc..28b42b2 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -9,6 +9,7 @@ #include #include "SnapClient.h" +#include "wifi.h" AudioBoardStream board(AudioKitEs8388V1); @@ -68,12 +69,36 @@ bool audioBeginMP3Stream(const char *code, Stream &source) { return true; } -bool audioPlayICY(const String &url) { +bool audioPlayInit(const char *code, const String &url, const bool needsWiFi, const bool needsSD) { audioStop(); - Serial.printf("[ICY] %s", url.c_str()); + Serial.printf("[%s] %s", code, url.c_str()); running = true; + if (needsWiFi) { + if (!isWifiConnected()) { + Serial.printf("[%s] WiFi not connected.\n", code); + audioStop(); + return false; + } + } + + if (needsSD) { + if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) { + Serial.printf("[%s] Failed to mount SD-card.\n", code); + audioStop(); + return false; + } + } + + return true; +} + +bool audioPlayICY(const String &url) { + if (!audioPlayInit("ICY", url, true, false)) { + return false; + } + if (!icy.begin(url.c_str())) { Serial.println("[ICY] Failed to start ICYStream."); audioStop(); @@ -83,14 +108,13 @@ bool audioPlayICY(const String &url) { return audioBeginMP3Stream("ICY", icy); } -bool audioPlaySNAP(const String &host) { - audioStop(); - - Serial.printf("[SNAP] %s\n", host.c_str()); - running = true; +bool audioPlaySNAP(const String &url) { + if (!audioPlayInit("SNAP", url, true, false)) { + return false; + } IPAddress ip; - if (!WiFiClass::hostByName(host.c_str(), ip)) { + if (!WiFiClass::hostByName(url.c_str(), ip)) { Serial.println("[SNAP] Failed to resolve host."); audioStop(); return false; @@ -119,14 +143,7 @@ bool audioPlaySNAP(const String &host) { } bool audioPlaySD(const String &url) { - audioStop(); - - Serial.printf("[SD] %s\n", url.c_str()); - running = true; - - if (!SD.begin(PIN_AUDIO_KIT_SD_CARD_CS)) { - Serial.println("[SD] Failed to mount SD-card."); - audioStop(); + if (!audioPlayInit("SD", url, false, true)) { return false; }