DFPlayer Mini MP3 module: how to play songs after each other

I am experimenting with an DFPlayer Mini MP3 module. I got it working and I can play one song. But here is the thing: I want to play multiple songs after each other. So first track 1 then track 2 and so on.

What happens is that track one playes for one second and then track two starts and plays for one second. And so on for next tracks.

Here is my code. What am I doing wrong?

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

static const uint8_t PIN_MP3_TX = 2; 
static const uint8_t PIN_MP3_RX = 3;

SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
DFRobotDFPlayerMini MP3player;

void setup()

{
  Serial.begin(9600);
  softwareSerial.begin(9600);
  if (MP3player.begin(softwareSerial))
  {
  Serial.println("MP3 Player ready");
  }
  else
  {
  Serial.println("MP3 Player NOT READY");
  }
  MP3player.volume(15);
  delay(500);
}

void loop()
{
  MP3player.play(1);
  delay(1000);
  MP3player.play(2);
  delay(1000);
  MP3player.play(3);
  delay(1000);
}
1 Like

After each play command, you have delay(1000), playing the track for one second which is 1000miliseconds, and then it goes to the next track and does the same. From looking at this code it looks like it's doing what you described. Were you hoping that it would play the entire track? If so you will need to get rid of the delay commands and determine a way of detecting when it has reached the end of the track to move to the next.

I took a quick look at the library and I see a function "isPlaying()". If you want the full track to play, try adding a test after calling play, that will check if the track is still playing. When the track is no longer playing, then you could call a delay(1000) if you want one second of silence before going to the next track

1 Like

Thank you LanGenz. Indeed I added a delay with the idea of having a pause between two tracks. No delay at all makes that the script runs so fast that no sound is audible.

I will go and try playing with the isPlaying function. If anyone has other information, please let me know.

See if this makes sense:
(uncompiled and untested)

int track = 0;
int numberOfTracks = 3;

void loop(){
   // if the player isn't playing
   if(!MP3player.isPlaying()){
       //  add one to track and loop back around if we're past the last one
      track++;
      if(track > numberOfTracks){
         track = 1;
      }
      // and play the next track
      MP3Player.play(track);
   }
   // If there's anything else you want to do while the track is playing
   //  like blink lights or move servos
   //  then do that here but don't use any blocking code. 
}

Thank you Delta_G, but unfortunately it does not work.

But I found something in this topic what does work.

My code is now:

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

static const uint8_t PIN_MP3_TX = 2; 
static const uint8_t PIN_MP3_RX = 3;

SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
DFRobotDFPlayerMini MP3player;

int i = 1;

void setup()
{
  Serial.begin(9600);
  softwareSerial.begin(9600);
  if (MP3player.begin(softwareSerial))
  {
  Serial.println("MP3 Player ready");
  }
  else
  {
  Serial.println("MP3 Player NOT READY");
  }
  MP3player.volume(10);
  delay(500);
}

void loop()
{
  MP3player.play(i);

  int state = MP3player.readState();
  while ( state == 529 || state == 513 || state == 512)
  { 
  state = MP3player.readState();
  //Serial.println(state);
  //Serial.println(MP3player.readType());
  delay(500);
  }
  i = i + 1;
}

Now I want to figure out what those numbers 529, 512 and 513 mean.

It wasn't meant to be something you could just drop in and it "work". It was meant to show how to use isPlaying to wait.

This code does the exact same thing with that cryptic call. The difference is that this code blocks any other code from running while you wait for the song to play. If you write that condition the other way round then you can let the loop keep going while the song plays.

Up to you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.