Web radio player for M5Stack and M5StickC in .m3u8 format
Support for .aac and .ts
Create a project and place this repository and the prerequisite library in the lib directory.
Call this library from main.cpp (see examples for details).
Create a sketch and place this repository and the prerequisite library in the Arduino/libraries/ directory.
Call this library from .ino file (see examples for details).
Constructors of M3U8Player class
If isAutoStart is set to true, then the instance starts to play automatically.
If isAutoStart is set to false, then you need to call start() explicitly.
M3U8Player(String url);
M3U8Player(String url, const float &startVolume);
M3U8Player(String url, const float &startVolume, const bool &isAutoStart);
M3U8Player(String url, const float &startVolume, const bool &isAutoStart, const uint32_t &bufferSize);
M3U8Player(String url, const float &startVolume, const bool &isAutoStart, const uint32_t &bufferSize, const bool &isCore2);
| argument | type | Description |
|---|---|---|
| url | String | URL of HLS playlist |
| startVolume | float | volume of initial setting (0-100, default 5) |
| isAutoStart | bool | start playback automatically or not (default false) |
| bufferSize | uint32_t | byte size of a buffer for audio data (default 4096) |
| isCore2 | bool | M5Stack Core2 or not (default false) |
example 1
#include <M3U8Player.h>
M3U8Player *player;
String stationUrl = "http://xxxx/xxxx/playlist.m3u8";
float defaultVolume = 10.0;
player = new M3U8Player(stationUrl, defaultVolume, true);example 2
#include <M3U8Player.h>
M3U8Player *player;
String stationUrl = "http://xxxx/xxxx/playlist.m3u8";
player = new M3U8Player(stationUrl);
player->start();The destructor of M3U8Player class
Start playback
If an argument isAutoStart of the constructor is set to true, then you do not need to call this member function.
bool start();
If the instance have already started playback, then returns false, otherwise true.
#include <M3U8Player.h>
M3U8Player *player;
String stationUrl = "http://xxxx/xxxx/playlist.m3u8";
player = new M3U8Player(stationUrl);
player->start();Change the volume
void setVolume(const float &newVolume);
| argument | type | Description |
|---|---|---|
| newVolume | float | volume (0.0-100.0) |
#include <M3U8Player.h>
M3U8Player *player;
String stationUrl = "http://xxxx/xxxx/playlist.m3u8";
float defaultVolume = 50.0;
player = new M3U8Player(stationUrl, defaultvolume, true);
if(M5.BtnA.wasPressed){
player->setVolume(70);
}Get the current volume value
float getVolume();
Current volume (0-100)
#include <M3U8Player.h>
M3U8Player *player;
float volume = player->getVolume();
Serial.println(volume);Change HLS playlist for playback
bool changeStationURL(const String &url);
| argument | type | Description |
|---|---|---|
| url | String | URL of HLS playlist |
If url doesn't start from http or https, then it returns false and doesn't change the URL, otherwise returns true and changes the URL.
#include <M3U8Player.h>
M3U8Player *player;
String initialUrl = "http://xxxx/xxxx/playlist.m3u8";
player = new M3U8Player(initialUrl, 10.0, true);
if(M5.BtnA.wasPressed){
String newUrl = "http://yyyy/yyyy/playlist.m3u8";
player->changeStationURL(newUrl);
}Get the URL of the HLS playlist that is currently being played back
String getStationURL();
URL of the HLS playlist that is currently being played back
#include <M3U8Player.h>
M3U8Player *player;
String currentUrl = player->getStationURL();
Serial.println(volume);Get current state
M3U8Player_State getState();
A state value which is represented by enam class M3U8Player_State
Values and desrciptions are below
| value | Description |
|---|---|
| SETUP | setup in progress |
| STANDBY | setup completed and able to start |
| PLAYING | currently playing back |
| CHANNEL_CHANGING | changing the URL of HLS playlist |
| RECOVERY_SEGMENT | rewinding the queue of segment URLs |
| OTHERS | the others |
#include <M3U8Player.h>
M3U8Player *player;
M3U8Player_State state = player->getState();
switch(state){
case M3U8Player_State::SETUP:
Serial.println("setup...");
break;
case M3U8Player_State::STANDBY:
Serial.println("standby now");
break;
case M3U8Player_State::PLAYING:
Serial.println("now playing");
break;
case M3U8Player_State::CHANNEL_CHANGING:
Serial.println("ch changing");
break;
case M3U8Player_State::RECOVERY_SEGMENT:
Serial.println("recovery segment");
break;
default:
Serial.println("something error");
}This is optional, but not required.
This will speed up the execution of channel changes.
In HTTPClient.cpp of arduino-esp32, rewrite the codes below
void HTTPClient::disconnect(bool preserveClient)
{
if(connected()) {
if(_client->available() > 0) {
log_d("still data in buffer (%d), clean up.\n", _client->available());
while(_client->available() > 0) { // from
_client->read(); // remove
} // to
}
// the rest omitted
}with
void HTTPClient::disconnect(bool preserveClient)
{
if(connected()) {
if(_client->available() > 0) {
log_d("still data in buffer (%d), clean up.\n", _client->available());
_client->flush(); // Add this line
}
// the rest omitted
}