MP3 player plays once but does not loop

For years I have successfully used mp3-tp-16p players. All of a sudden none of my sketches loop. It is very strange. I have tried using half a dozen different boards and several different sketches.
One song plays then the sketch stops just before the tune is to be played again in the following loop. The following just prints "Music plays" every 100 milliseconds.
I have been scratching my head for days and tempted to give up. Any tip on what I am doing wrong?

/// MP3
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX10,11
DFRobotDFPlayerMini myDFPlayer;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  Serial.println("music plays");
  delay(100);
  myDFPlayer.volume(20);///set volume to 30
  myDFPlayer.play(001);
  delay(1000);
  Serial.println("music has been played");
  delay(100);
}

You have not initialised the player. At the very least include the line I've added.

And that fixed volume setting should be done in setup().


//#include "Arduino.h" // Redundant
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(8, 9); // RX, TX, my breadboard
// SoftwareSerial mySoftwareSerial(10, 11); // RX, TX10,11
DFRobotDFPlayerMini myDFPlayer;


void setup()
{
  Serial.begin(115200); // my preference

  mySoftwareSerial.begin(9600); 

  myDFPlayer.begin(mySoftwareSerial); // Start communication with DFPlayer
  
  myDFPlayer.volume(20);///set volume to 30
  delay(1000);

}

void loop()
{
  Serial.println("music plays");
  delay(100);
  myDFPlayer.play(001);
  delay(1000); // Assuming  1 sec is longe enough
  Serial.println("music has been played");
  delay(100);
}
1 Like

Thank you Terrypin but it still just runs once. This is what the serial monitor reads:
Music plays
music has played
music plays

and then nothing. It is the same as before.
I copied your code exactly.
Thanks, I appreciate you trying to help.
It all went wrong after I updated to arduino 2.2.1 but I have tried using earlier versions without success. I have been using the mp3 players for years. I am at a loss.

This behavior is expected. Many arduino sketches do not work properly/unstable/do not compile, as IDE and libraries develop thru time. Propably, if you go back to your old IDE and library, it works perfectly fine. When I code MCUs using Arduino IDE, i write the IDE and library version in the comments. So, it may work and compile even after 10 years, i just need to know what it was compibled with. One famous example is the PING sketch with the ethernet.h library. After the ethernet.h "update" this sketch was rendered usless and the last time i checked, nobody was able to make a stable/working PING sketch with the new library.

1 Like

I assume you changed the Rx/Tx pins back to your own?

1 Like

Give this a try

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

// Conditional compilation for the serial port
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))   // Using a soft serial port
#include <SoftwareSerial.h>
SoftwareSerial softSerial(/*rx =*/4, /*tx =*/5);
#define FPSerial softSerial
#else
#define FPSerial Serial1
#endif

// Declare the DFPlayer object
DFRobotDFPlayerMini myDFPlayer;

void setup() {
  // Initialize the serial port
  #if (defined ESP32)
    FPSerial.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
  #else
    FPSerial.begin(9600);
  #endif

  // Initialize the Serial for debugging
  Serial.begin(115200);

  // Print initialization messages
  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take  3~5 seconds)"));

  // Initialize DFPlayer
  if (!myDFPlayer.begin(FPSerial, /*isACK = */true, /*doReset = */true)) {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true);
  }
  Serial.println(F("DFPlayer Mini online."));

  // Set DFPlayer configuration
  myDFPlayer.setTimeOut(500); // Set serial communication timeout to  500ms
  myDFPlayer.volume(20); // Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); // Set EQ to normal
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Set SD as the output device
  myDFPlayer.loop(1); // Loop the first mp3
}

void loop() {
  // Check for available commands from DFPlayer
  if (myDFPlayer.available()) {
    uint8_t type = myDFPlayer.readType();
    int value = myDFPlayer.read();

    // Handle playback completion
    if (type == DFPlayerPlayFinished) {
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      myDFPlayer.play(1); // Play the first mp3 again
    }
  }
}
1 Like

Thank you all. I uninstalled 2.2.1 and it still did not work. I tried three players with no luck but the forth worked!
I think all three mp3 players must have been damaged.
Thank you all for helping me problem solve this. Although no-one in particular solved it you all assisted by getting me to think outside the square. I greatly appreciate your help.
Boy I have spent some time trying to fix this :frowning:

Yes, these modules do have have frustrating inconsistencies. As you saw, I deliberately changed only one line in your original sketch. But before finally dumping the other three modules, try substituting my code below for the corresponding initialisation lines, using each of those suspects. Maybe we can salvage one or more of them.

`if (!player.begin(softwareSerial, true, false))
  {
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  }
  Serial.println(F("DFPlayer Mini online."));
  player.setTimeOut(1000);
  //----Set volume----
  player.volume(20); // BTW, 30 can cause voltage drop
  // issue in some circuits; have not seen yours.
  delay(500);

  Serial.println("end setup"); // No crash so far
} `

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