DFMini Player and a Nano, loop an MP3 in the background

Greetings! I hope this is the correct place to post this topic. I'm a beginner and I'm building a Nano lightsaber using the DFMini player as my audio source. I'm trying to loop the "hum" in the background but the DFMini library files I've tried to use don't seem to work. I know it's possible as I've seen examples posted.

A few months ago I used the Adafruit soundboard in a Portal Gun and the library called for sfx.unpaused so it would loop the sound over and over in the background until it was interrupted by a button. When I try to loop the DFMini MP3 file, it only plays the first 5ms and makes a "tat-tat-tat-tat-tat" sound as it's starting the mp3 over and over without it playing. (mp3.playTrack) I've added a delay (2000) but then the buttons are unresponsive until the delay is up. Am I'm missing something? Are these the wrong Libraries?

The DFMini libraries I've used are:
DFMiniMP3-1.0
DFPlayer_Mini_MP3
DFRobotDFPlayerMini

Any help pointing me in the right direction would be great! Thank you.

The hardware is hooked up correctly, I think it's the software or library syntax.
Here is the basic code:

#include <DFMiniMp3.h>
#include <Wire.h> // Include the I2C library (required)
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>

SoftwareSerial secondarySerial(11, 10); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

void setup() {
secondarySerial.begin(9600);//
Serial.begin(9600);//
mp3.begin();
mp3.setSerial (Serial); //set Serial for DFPlayer-mini mp3 module
mp3.setVolume(30);
}

void loop() {
mp3.playMp3FolderTrack(03); // play waking up comment
}

So that just issues the playtrack command over and over and over.
It doesn't wait for it to get done.

myDFPlayer.loop(3);

Ah. So if LOOP is enabled, will it get interrupted if I push a button and add mp3.playMp3FolderTrack(01); delay (500);? Or would I need to add disableLoop first?

Sorry for the noob questions. Audio life was easier with the Adifruit Soundboard, but it's on 16MB.

Interesting.
If there's a push button that could trigger a bool flag where then the .loop play would execute based on the status of that bool flag.
Or if it was in setup() then it would only go off once, if that's all you want to do.

Interesting. I'll have to pull apart the Portal Gun code. Basically, It's a lightsaber that when a button is pushed, play the "Extend" sound and then loops the "hum." Push the button again and the "Contract" sound plays.

The Portal Gun code would play the "online idle" sound nonstop until you pushed a button. The "online idle" sound was like 10 seconds, but any push of a button would interrupt the audio and perform the next action, which would also light up and play a sound, then revert back to the "online idle" sound. The DFPlayer is different but I think I'm getting it.

I hope that makes sense ha.

The player has a Busy pin - which can be monitored (indicates whether it's playing a track).

Haha, I literally just saw that from the Portal code. The soundboard is ACT and given an INT variable.

CODE with the Soundboard:
const int ACT = 13;
char hum[] = "T01 WAV";
pinMode(ACT, INPUT);

void playAudio( char* trackname, int playing ) {
// stop track if one is going
if (playing == 0) {
sfx.stop();
}
// now go play
if (sfx.playTrack(trackname)) {
sfx.unpause();
}
}

void loop() {
int playing = digitalRead(ACT);
playAudio(hum, playing);
}

So I bet there's a way to convert this code to DFMini's syntax.

@mrrsj here is another library you could try:

It has a function called loop that takes a single track number. I don't know if the implementation would introduce a period of silence as it restarts the track.

Another option would be to create one long mp3 track that has, say, 1 hour of your "hum" sound and play that track knowing that it will always be long enough without any repeating needed.

I actually thought about creating an hour MP3 file, but would giving it the loop(1) command keep it going until another sound stopped it? I'm only wanting it to loop once a button is activated. I'm not at home at the moment to test. I'm gathering notes before I start.

Thank you for the link!

// See thread https://forum.arduino.cc/t/dfmini-player-and-a-nano-loop-an-mp3-in-the-background/1172365

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

SoftwareSerial mySerial(8, 9); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

void setup()
{
  Serial.begin(115200);
  delay(200);

  mySerial.begin(9600);
  myDFPlayer.begin(mySerial); // Communicate with DFPlayer
  myDFPlayer.volume(15); // 0-30

  ////////////// COULD PLAY FILE(s) HERE /////////////////
  
  //  for ( i = 1; i < 11; i++)
  //  {
  //    Serial.println(i);
  //    myDFPlayer.play(i);
  //    delay(1000);
  //  }

  //      while (1 == 1); // Stops program from proceeding
} // End setup
///////////////////////////////////////////////////////////
void loop()
{
  // This will simply play the file repeatedly
  // Substitute your choice and add your required logic
  // DFR Player is fussy about micro SD file structure; this MP3 is just
  // one I have at hand in the root of a micro SD card, and is about 900 ms
  myDFPlayer.play(137);
  delay(1000);
} // End of loop

Nice! I'm also trying not to use a delay command in the loop to avoid long response times when a button is pushed.

I've tried several libraries for this module but the one used in post #12 is my clear preference.

Re your post earlier, note that it has a loop function. Here's a short list of some other functions.

myDFPlayer.loop(1);  //Loop the first
myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
myDFPlayer.next();  //Play next mp3
myDFPlayer.pause();  //pause the mp3
myDFPlayer.play(1);  //Play the first mp3

I'm not 100% sure of your objective, so please advise if it's roughly as follows:
After power-up, continually play a long (?) MP3 or WAV of a steady sound until a low-going button is pressed?

No change in its content or volume? No restarting on the next press of the same button? Or another button? Or by time-based logic? Etc, etc.

Anyway, hope my simple sketch provides a useful base for experiment.

Greatly appreciate it! The function is as follows:

If Button is pushed, extend NeoPixels and play the saber extend sound. Set STATE to 1.
If STATE is 1, then play a looping lightsaber hum.
I do have an accelerometer, so if there is movement, play a swing sound. Revert back to the hum.
If Button is pushed again, stop the hum and play the saber contract sound.

Having the accelerometer makes it difficult to have the delay function for the looping sound.

Hopefully my sketch can be useful as a base for the DFR player functions amongst the tasks you list.

I prepared
this demonstration sketch
for a somewhat similar Halloween project, post #26. Try breadboarding it to see if it helps.

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