How to handle redirected streams?

Most of my favorite streams use URL’s with temporarily redirected streams. This can be seen in Firefox, but ESPMUSE cannot handle this. For example if i enter in a browser: “http://liveradio.swr.de/sw282p3/swr1rp/”, this will be changed to: “http://d131.rndfnk.com/ard/swr/swr1/rp/mp3/128/stream.mp3?aggregator=web&sid=28YZHKYJReTZnGEb7VOcc8DtOSd&token=QMdrPGCq06uNk7n0kSbdeqEKNrLH6xmSlSLqKnVwLjE&tvf=yYaF9AAD6xZkMTMxLnJuZGZuay5jb20” and plays the music. But after some time this will not work any more - neither in the browser nor in ESPMUSE, because this redirection is temporarily (HTTP-Code 302)! Therefore, the question is: How to implement this redirection with ESPMUSE?

Hi Mp3fan and welcome,

Your question is how to handle redirected stream URLs on ESP32, it seems that this library handle it : https://github.com/schreibfaul1/ESP32-audioI2S/wiki

Hi Raspiaudio,
thank you for your attention!
I think this is not what i wanted. I want to follow an redirection with HTTP-Code 302, which is initiated by the server. With the function you metioned you can get the previous station an user selected.

I found an example “StreamHttpClient” included in the ESP32-Library
(Examples/ESP32 Dev Module/HTTPClient/StreamHttpClient)
in the Arduino-IDE. And i found


If you insert one line
“http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);”
after the already exsting line “HTTPClient http;” in the example “StreamHttpClient.ino”, the 302-redirect is done. But the data are only printed to USB-Serial. There is no mp3-decoding and you can hear nothing. Unfortunately the ESPMUSE-libraries do not use
#include <HTTPClient.h>.
Therefore, this is not the solution, but it shows the goal.

Currently i try some additions to the internet-radio-software to handle redirected streams:

/// in Include-Section:

#include ...
#include <HTTPClient.h>

/// End Include-Section<<<


// in playRadio: change one Line, other lines unhanged as before
static void playRadio(void* ){
	.
	.
	.
	audio.connecttohost(getPlayURL(linkS));  // Change:insert "getPlayURL"
	.
	.
	.
}

//Completely new:
///////////////////////////////////////////////////////////////////////
//  Redirect check: getPlayURL
/////////////////////////////////////////////////////////////////////////
String getPlayURL(String sURL) {
  HTTPClient http; 
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // Redirect Option
  String Location = sURL;
  http.begin(sURL);
  int httpCode = http.GET();
  if (httpCode == HTTP_CODE_OK) {
    Location = http.getLocation();
  }
  if (0 == Location.length()) {
    Location = sURL;
  }
  Serial.println("sURL ----::");
  Serial.println(sURL);
  Serial.println("_____________");
  Serial.println("http.getLocation()::::");
  Serial.println(Location);
  Serial.println(">>>>>>>>");
  return Location;
}