Reading and playing MP3 from SD-card

Hello,
I've been working on a project for more than a week that reads an MP3 file from the SD card and outputs it to a speaker. The sound quality isn't that important, it's just supposed to be the whistle of a steam locomotive.
The module is an ESP32 CAM.
Only a limited number of pins are still free.
These are :
GPIO12, GPIO15, GPIO14, GPIO02, GPIO04.
I can use either a DAC I2S PCM5102 or a LM386 audio amplifier.
How do I have to add the code so that I can output the MP3 sound to a speaker?
Can someone please help me?
Many thanks in advance.
This is the code

/*
Project:  ESP32-CAM handle SD-Card
Author:   Kurt Struevy
Date:     Created 01.06.2023
Version:  V1.0
IDE:      Arduino IDE 2.1.0
 
Required Board (Tools -> Board -> Boards Manager...)
 - Board: AI Thinker ESP32-CAM
*/

#include "SD_MMC.h"

void setup() 
{
  Serial.begin(115200);
  pinMode(4, OUTPUT);
  // Mounten der SD-Karte
  if(!SD_MMC.begin()) 
  {
    Serial.println("Kann die SD-Karte nicht mounten");
    return;
  }
  
  uint8_t cardType = SD_MMC.cardType();
  if(cardType == CARD_NONE) 
  {
    Serial.println("Keine SD-Karte eingelegt");
    return;
  }
  Serial.printf("Kartentyp = %d\n",cardType);
  listDir(SD_MMC,"/",0);
 }

void listDir(fs::FS &fs, const char * dirname, uint8_t levels)
{
  Serial.printf("Listing directory: %s\n", dirname);
  File root = fs.open(dirname);
  if(!root)
  {
    Serial.println("Failed to open directory");
    return;
  }
  if(!root.isDirectory())
  {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while(file)
  {
    if(file.isDirectory()){
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if(levels)
      {
        listDir(fs, file.name(), levels -1);
      }
    } 
    else 
    {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
    }
     file = root.openNextFile();
  }
}

void loop() 
{
  //nothing to do
}

And this is the output on the serial monitor:

Kartentyp = 3
Listing directory: /
  DIR : System Volume Information
  DIR : MP3
  DIR : Pictures
  FILE: LoveLetter.txt  SIZE: 12

I am also building a remote control train. I am looking to also play a sound file with the esp32-cam. I found a way for the non-camera version but no luck so far on this one. I hope someone may have some insight into having this work. The project I am using is at https://www.diymachines.co.uk/wifi-3d-printed-steam-train-with-live-view-camera

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