Advertisement
NittyGritty

ESP8266-DF-PlayMP3.ino

Nov 10th, 2021
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. // this example will play a track and then
  2. // every five seconds play another track
  3. //
  4. // it expects the sd card to contain these three mp3 files
  5. // but doesn't care whats in them
  6. //
  7. // sd:/mp3/0001.mp3
  8. // sd:/mp3/0002.mp3
  9. // sd:/mp3/0003.mp3
  10.  
  11. #include <SoftwareSerial.h>
  12. #include <DFMiniMp3.h>
  13.  
  14. // implement a notification class,
  15. // its member methods will get called
  16. //
  17. class Mp3Notify
  18. {
  19. public:
  20.   static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
  21.   {
  22.     if (source & DfMp3_PlaySources_Sd)
  23.     {
  24.         Serial.print("SD Card, ");
  25.     }
  26.     if (source & DfMp3_PlaySources_Usb)
  27.     {
  28.         Serial.print("USB Disk, ");
  29.     }
  30.     if (source & DfMp3_PlaySources_Flash)
  31.     {
  32.         Serial.print("Flash, ");
  33.     }
  34.     Serial.println(action);
  35.   }
  36.   static void OnError(uint16_t errorCode)
  37.   {
  38.     // see DfMp3_Error for code meaning
  39.     Serial.println();
  40.     Serial.print("Com Error ");
  41.     Serial.println(errorCode);
  42.   }
  43.   static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
  44.   {
  45.     Serial.print("Play finished for #");
  46.     Serial.println(track);  
  47.   }
  48.   static void OnPlaySourceOnline(DfMp3_PlaySources source)
  49.   {
  50.     PrintlnSourceAction(source, "online");
  51.   }
  52.   static void OnPlaySourceInserted(DfMp3_PlaySources source)
  53.   {
  54.     PrintlnSourceAction(source, "inserted");
  55.   }
  56.   static void OnPlaySourceRemoved(DfMp3_PlaySources source)
  57.   {
  58.     PrintlnSourceAction(source, "removed");
  59.   }
  60. };
  61.  
  62. // instance a DFMiniMp3 object,
  63. // defined with the above notification class and the hardware serial class
  64. //
  65. // DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial1);
  66.  
  67. // Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
  68. // comment out the above definition and uncomment these lines
  69. SoftwareSerial secondarySerial(D7, D6); // RX, TX
  70. DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);
  71.  
  72. void setup()
  73. {
  74.   Serial.begin(115200);
  75.  
  76.   Serial.println("initializing...");
  77.  
  78.   mp3.begin();
  79.  
  80.   uint16_t volume = mp3.getVolume();
  81.   Serial.print("volume ");
  82.   Serial.println(volume);
  83.   mp3.setVolume(15);
  84.  
  85.   uint16_t count = mp3.getTotalTrackCount(DfMp3_PlaySource_Sd);
  86.   Serial.print("files ");
  87.   Serial.println(count);
  88.  
  89.   Serial.println("starting...");
  90. }
  91.  
  92. void waitMilliseconds(uint16_t msWait)
  93. {
  94.   uint32_t start = millis();
  95.  
  96.   while ((millis() - start) < msWait)
  97.   {
  98.     // calling mp3.loop() periodically allows for notifications
  99.     // to be handled without interrupts
  100.     mp3.loop();
  101.     delay(1);
  102.   }
  103. }
  104.  
  105. void loop()
  106. {
  107.   uint16_t volume;
  108.   Serial.println("------------------");
  109.   Serial.println("track 1");
  110.   mp3.playMp3FolderTrack(1);  // sd:/mp3/0001.mp3
  111.  
  112.   waitMilliseconds(2000);
  113.  
  114.   volume = mp3.getVolume();
  115.   Serial.print("volume ");
  116.   Serial.println(volume);
  117.   mp3.setVolume(15);
  118.  
  119.   Serial.println("track 2");
  120.   mp3.playMp3FolderTrack(2); // sd:/mp3/0002.mp3
  121.  
  122.   waitMilliseconds(2000);
  123.  
  124.   volume = mp3.getVolume();
  125.   Serial.print("volume ");
  126.   Serial.println(volume);
  127.   mp3.setVolume(15);
  128.  
  129.   Serial.println("track 3");
  130.   mp3.playMp3FolderTrack(3); // sd:/mp3/0002.mp3
  131.  
  132.   waitMilliseconds(2000);
  133.  
  134.   volume = mp3.getVolume();
  135.   Serial.print("volume ");
  136.   Serial.println(volume);
  137.   mp3.setVolume(15);
  138.  
  139.  
  140.   Serial.println("track 091 from folder 01");
  141.   mp3.playFolderTrack(1, 91); // sd:/01/091.mp3
  142.  
  143.   waitMilliseconds(5000);
  144.  
  145.   volume = mp3.getVolume();
  146.   Serial.print("volume ");
  147.   Serial.println(volume);
  148.   mp3.setVolume(15);
  149.  
  150.   Serial.println("track 092 from folder 01");
  151.   mp3.playFolderTrack(1, 92); // sd:/01/092.mp3
  152.  
  153.   waitMilliseconds(5000);
  154.  
  155.   volume = mp3.getVolume();
  156.   Serial.print(">volume ");
  157.   Serial.println(volume);
  158.   mp3.setVolume(15);
  159.  
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement