NittyGritty icon

ESP8266-DF-PlayMP3.ino

NittyGritty | PRO | 11/10/21 06:28:01 PM UTC | 0 ⭐ | 1504 👁️ | Never ⏰ | []
C++ |

3.76 KB

|

None

|

0 👍

/

0 👎

// this example will play a track and then 
// every five seconds play another track
//
// it expects the sd card to contain these three mp3 files
// but doesn't care whats in them
//
// sd:/mp3/0001.mp3
// sd:/mp3/0002.mp3
// sd:/mp3/0003.mp3
 
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
 
// implement a notification class,
// its member methods will get called 
//
class Mp3Notify
{
public:
  static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
  {
    if (source & DfMp3_PlaySources_Sd) 
    {
        Serial.print("SD Card, ");
    }
    if (source & DfMp3_PlaySources_Usb) 
    {
        Serial.print("USB Disk, ");
    }
    if (source & DfMp3_PlaySources_Flash) 
    {
        Serial.print("Flash, ");
    }
    Serial.println(action);
  }
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
  static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
  {
    Serial.print("Play finished for #");
    Serial.println(track);  
  }
  static void OnPlaySourceOnline(DfMp3_PlaySources source)
  {
    PrintlnSourceAction(source, "online");
  }
  static void OnPlaySourceInserted(DfMp3_PlaySources source)
  {
    PrintlnSourceAction(source, "inserted");
  }
  static void OnPlaySourceRemoved(DfMp3_PlaySources source)
  {
    PrintlnSourceAction(source, "removed");
  }
};
 
// instance a DFMiniMp3 object, 
// defined with the above notification class and the hardware serial class
//
// DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);
 
// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
SoftwareSerial secondarySerial(D7, D6); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
 
void setup() 
{
  Serial.begin(115200);
 
  Serial.println("initializing...");
  
  mp3.begin();
 
  uint16_t volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(15);
  
  uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
  Serial.print("files ");
  Serial.println(count);
  
  Serial.println("starting...");
}
 
void waitMilliseconds(uint16_t msWait)
{
  uint32_t start = millis();
  
  while ((millis() - start) < msWait)
  {
    // calling mp3.loop() periodically allows for notifications 
    // to be handled without interrupts
    mp3.loop(); 
    delay(1);
  }
}
 
void loop() 
{
  uint16_t volume;
  Serial.println("------------------");
  Serial.println("track 1"); 
  mp3.playMp3FolderTrack(1);  // sd:/mp3/0001.mp3
  
  waitMilliseconds(2000);
 
  volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(15);
  
  Serial.println("track 2"); 
  mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
  
  waitMilliseconds(2000);
 
  volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(15);
  
  Serial.println("track 3");
  mp3.playMp3FolderTrack(3); // sd:/mp3/0002.mp3
  
  waitMilliseconds(2000); 
 
  volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(15);
 
 
  Serial.println("track 091 from folder 01"); 
  mp3.playFolderTrack(1, 91); // sd:/01/091.mp3
 
  waitMilliseconds(5000); 
 
  volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(15);
 
  Serial.println("track 092 from folder 01"); 
  mp3.playFolderTrack(1, 92); // sd:/01/092.mp3
  
  waitMilliseconds(5000); 
 
  volume = mp3.getVolume();
  Serial.print(">volume ");
  Serial.println(volume);
  mp3.setVolume(15);
  
}
 

Comments