I have a music player via an ESP32 and an SD card module
Structure:
SD card module (with the .wav files) -> ESP32
ESP32 pin 26 to Audio amplifier module LM386 IN (with 10µF capacitor between IN and GND) and ESP32 GND to Audio amplifier module LM386 GND and from Audio amplifier module LM386 GND and VCC to the external power supply constant 5V
From audio amplifier module GND and OUT to the loudspeaker (8 Ohm 100W)
Problem:
When I want to play the .wav files from the SD card via the ESP32, I always get a loud noise when it plays and only the music of the .wav file in the background
Can anyone help me?
Code (is just the code snippet):
bool startAudioPlayback(const String& pfad) {
// Vorhandene Instanzen löschen, um Speicherlecks zu vermeiden
if (wav != nullptr) {
wav->stop();
delete wav;
wav = nullptr;
}
if (file != nullptr) {
delete file;
file = nullptr;
}
if (out != nullptr) {
delete out;
out = nullptr;
}
// Neue Instanzen erstellen
out = new AudioOutputI2S();
out->SetPinout(27, 26, 26); // Anpassen der Pins je nach ESP32 Setup
out->SetGain(0.1); // Setzt die Lautstärke auf 10%
out->SetRate(480000); // Common sample rates are 44100 or 48000 Hz
out->SetBitsPerSample(16); // Use 16 for CD quality audio
out->SetChannels(2); // Set to 2 for stereo
file = new AudioFileSourceSD(pfad.c_str()); // Dateipfad als C-String übergeben
if (!file->isOpen()) {
Serial.println("Fehler: Datei konnte nicht geöffnet werden");
delete file;
file = nullptr;
delete out;
out = nullptr;
return false;
}
wav = new AudioGeneratorWAV();
bool success = wav->begin(file, out);
if (success) {
Serial.println("Spiele Audio ab...");
while (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
}
delay(1); // Kürzere Verzögerung, um die Schleife effizienter zu machen
}
Serial.println("Audiowiedergabe abgeschlossen.");
} else {
Serial.println("Fehler beim Starten der Audiowiedergabe");
}
// Instanzen löschen
delete wav;
delete file;
delete out;
// Instanzen auf NULL setzen, um wiederholte Zugriffe zu verhindern
wav = nullptr;
file = nullptr;
out = nullptr;
return success;
}