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.
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
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.
}
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.