Question of DFPlayerError: 126 - Unknown error

#include <DFRobotDFPlayerMini.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SD.h>

HardwareSerial mySerial(1); // 使用Serial1
DFRobotDFPlayerMini dfPlayer;

const int prevButtonPin = 12; // 前一首按钮引脚
const int pauseButtonPin = 13; // 暂停按钮引脚
const int nextButtonPin = 14; // 后一首按钮引脚
const int busyPin = 5; // DFPlayer Mini的Busy引脚

LiquidCrystal_I2C lcd(0x27, 16, 2); // 初始化LCD,地址0x27,16列2行

int totalTracks = 0;
int currentTrack = 1;
bool isPaused = false;

void setup() {
pinMode(prevButtonPin, INPUT_PULLUP);
pinMode(pauseButtonPin, INPUT_PULLUP);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(busyPin, INPUT_PULLUP);

Serial.begin(2400);
mySerial.begin(9600, SERIAL_8N1, 16, 17); // RX = 16, TX = 17 for DFPlayer

lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");

if (!dfPlayer.begin(mySerial)) {
lcd.setCursor(0, 1);
lcd.print("Init failed!");
Serial.println("Unable to begin:");
Serial.println("1. Please recheck the connection!");
Serial.println("2. Please insert the SD card!");
while (true);
}

Serial.println("DFPlayer Mini online.");
lcd.setCursor(0, 0);
lcd.print("DFPlayer online");

totalTracks = dfPlayer.readFileCounts();
Serial.print("Total tracks: ");
Serial.println(totalTracks);

if (totalTracks == -1) {
Serial.println("Read SD card error!");
lcd.setCursor(0, 1);
lcd.print("Read SD error!");
} else {
Serial.println("SD card read successfully!");
lcd.setCursor(0, 1);
lcd.print("Tracks: ");
lcd.print(totalTracks);
}

dfPlayer.volume(5); // 设置音量(0-30)
playTrack(currentTrack);
}

void loop() {
if (digitalRead(prevButtonPin) == LOW) {
previousTrack();
delay(200); // 防抖动
}

if (digitalRead(nextButtonPin) == LOW) {
nextTrack();
delay(200); // 防抖动
}

if (digitalRead(pauseButtonPin) == LOW) {
pauseTrack();
delay(200); // 防抖动
}

if (digitalRead(busyPin) == LOW) {
nextTrack();
}

// 检查是否有DFPlayer错误
if (dfPlayer.available()) {
printDetail(dfPlayer.readType(), dfPlayer.read());
}

delay(100); // 延迟以便更好地观察输出信息
}

void playTrack(int track) {
dfPlayer.play(track);
lcd.setCursor(0, 1);
lcd.print("Playing: ");
lcd.print(track);
Serial.print("Playing track: ");
Serial.println(track);
}

void nextTrack() {
if (currentTrack < totalTracks) {
currentTrack++;
} else {
currentTrack = 1; // 循环播放
}
playTrack(currentTrack);
}

void previousTrack() {
if (currentTrack > 1) {
currentTrack--;
} else {
currentTrack = totalTracks; // 循环播放
}
playTrack(currentTrack);
}

void pauseTrack() {
if (isPaused) {
dfPlayer.start();
lcd.setCursor(0, 1);
lcd.print("Playing: ");
lcd.print(currentTrack);
Serial.println("Resuming track.");
} else {
dfPlayer.pause();
lcd.setCursor(0, 1);
lcd.print("Paused: ");
lcd.print(currentTrack);
Serial.println("Pausing track.");
}
isPaused = !isPaused;
}

void printDetail(uint8_t type, int value) {
switch (type) {
case DFPlayerPlayFinished:
Serial.print("Number:");
Serial.print(value);
Serial.println(" Play finished.");
nextTrack(); // 播放完自动播放下一首
break;
case DFPlayerError:
Serial.print("DFPlayerError: ");
Serial.print(value); // 打印错误码
Serial.print(" - ");
switch (value) {
case Busy:
Serial.println("Card not found");
break;
case Sleeping:
Serial.println("Sleeping");
break;
case SerialWrongStack:
Serial.println("Stack wrong");
break;
case CheckSumNotMatch:
Serial.println("Check sum not match");
break;
case FileIndexOut:
Serial.println("File index out of bounds");
break;
case FileMismatch:
Serial.println("Cannot find file");
break;
case Advertise:
Serial.println("In advertise");
break;
default:
Serial.println("Unknown error");
break;
}
break;
default:
break;
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Please confirm that you are using a Nano ESP32 board and not a more generic ESP32 board

is nodemcu-32

So not a Nano ESP32 even though you posted in that category of the forum

Your topic has been moved to a more generic category

Please add code tags to your original post

I have solved it myself. Thank you.

For the benefit of other forum member please share what the problem was and your solution