ESP32-S3 - No playing sound

headquaker
Posts: 20
Joined: Thu Sep 21, 2023 6:28 am

ESP32-S3 - No playing sound

Postby headquaker » Thu Sep 21, 2023 12:51 pm

Hello,
I don't understand why my code is not working.
The mp3 is not played and the name of the musics are displayed on the screen but very very fast.
Do you have any idea?
here my code :

Code: Select all

#include "Arduino.h"
#include "Audio.h"
#include "FS.h"
#include "SD_MMC.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// Pin definitions
#define SD_MMC_CMD 38
#define SD_MMC_CLK 39
#define SD_MMC_D0  40
#define I2S_BCLK   14
#define I2S_DOUT   13
#define I2S_LRC    12
#define SDA 19                    // Define SDA pins
#define SCL 20                    // Define SCL pins
#define PIN_BUTTON 5

Audio audio;
LiquidCrystal_I2C lcd(0x27,16,2); 
File root;
File currentFile;

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

  // Initialize the I2C LCD
  Wire.begin(SDA, SCL);
  if (!i2CAddrTest(0x27)) {
    lcd = LiquidCrystal_I2C(0x3F, 16, 2);
  }
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Croc Blanc");

  // Initialize the SD card
  SD_MMC.setPins(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
  if (!SD_MMC.begin("/sdcard", true, true, SDMMC_FREQ_DEFAULT, 5)) {
    Serial.println("Card Mount Failed");
    return;
  }
  root = SD_MMC.open("/");
  if (!root) {
    Serial.println("Failed to open root directory");
    return;
  }

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(9);  // Adjust the volume as needed

  playNextMP3();
}

void loop() {
  audio.loop();
  if (digitalRead(PIN_BUTTON) == LOW) {
    delay(50);  // Debounce
    playNextMP3();
  }
}

void playNextMP3() {
  if (currentFile) {
    currentFile.close();
  }

  File entry;
  while ((entry = root.openNextFile())) {
    if (!entry.isDirectory() && entry.name()) {
      currentFile = entry;

      // Display the file name on the LCD
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Now Playing:");
      lcd.setCursor(0, 1);
      lcd.print(currentFile.name());

      // Play the MP3 file
      audio.connecttoFS(SD_MMC, currentFile.name());
      return;
    }
  }

  // No more MP3 files to play, rewind to the beginning
  root.rewindDirectory();
}

bool i2CAddrTest(uint8_t addr) {
  Wire.begin();
  Wire.beginTransmission(addr);
  if (Wire.endTransmission() == 0) {
    return true;
  }
  return false;
}

headquaker
Posts: 20
Joined: Thu Sep 21, 2023 6:28 am

Re: ESP32-S3 - No playing sound

Postby headquaker » Sat Sep 23, 2023 9:25 pm

OK I managed to make it works
The last issue is the pause/play mode that I cannot find who to do.

Next/Previous/Volume UP & Down are OK

Code: Select all

#include "Arduino.h"
#include "Audio.h"
#include "FS.h"
#include "SD_MMC.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <vector>

// Pin definitions
#define SD_MMC_CMD 38
#define SD_MMC_CLK 39
#define SD_MMC_D0  40
#define I2S_BCLK   14
#define I2S_DOUT   13
#define I2S_LRC    12
#define SDA 19                    // Define SDA pins
#define SCL 20                    // Define SCL pins
#define IR_RECEIVER_PIN 21        // Infrared receiving pin

const unsigned long VOL_CHANGE_DELAY = 1000; // Delay between volume changes
const unsigned long DISPLAY_VOLUME_DURATION = 3000; // Display volume for 3 seconds

Audio audio;
LiquidCrystal_I2C lcd(0x27, 16, 2);
File root;
std::vector<String> mp3Files;
int currentSongIndex = -1;
IRrecv irrecv(IR_RECEIVER_PIN);
decode_results results;

unsigned long lastVolChangeTime = 0;
unsigned long volumeDisplayEndTime = 0;
bool displayingVolume = false;

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

  irrecv.enableIRIn();        // Start the receiver

  // Initialize the I2C LCD
  Wire.begin(SDA, SCL);
  if (!i2CAddrTest(0x27)) {
    lcd = LiquidCrystal_I2C(0x3F, 16, 2);
  }
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Loading musics...");

  // Initialize the SD card
  SD_MMC.setPins(SD_MMC_CLK, SD_MMC_CMD, SD_MMC_D0);
  if (!SD_MMC.begin("/sdcard", true, true, SDMMC_FREQ_DEFAULT, 5)) {
    Serial.println("Card Mount Failed");
    return;
  }
  root = SD_MMC.open("/");
  if (!root) {
    Serial.println("Failed to open root directory");
    return;
  }

  // Load the list of MP3 files in the root directory
  loadMP3Files();

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(9);  // Adjust the initial volume as needed

  playNextMP3();
}

void loop() {
  audio.loop();
  if (irrecv.decode(&results)) {
    handleControl(results.value);
    irrecv.resume();
  }

  // Check if we should display the volume
  if (displayingVolume && millis() > volumeDisplayEndTime) {
    displayingVolume = false;
    displayNowPlaying();
  }
}

void loadMP3Files() {
  mp3Files.clear();
  File entry;
  while ((entry = root.openNextFile())) {
    if (!entry.isDirectory() && entry.name() && String(entry.name()).endsWith(".mp3")) {
      mp3Files.push_back(String(entry.name()));
    }
  }
  root.rewindDirectory();
}

void handleControl(unsigned long value) {
  switch (value) {
    case 0xFF906F: // Next
      delay(50);  // Debounce
      playNextMP3();
      break;
    case 0xFFE01F: // Previous
      delay(50);  // Debounce
      playPreviousMP3();
      break;
    case 0xFF9867: // Volume Down
      if (millis() - lastVolChangeTime > VOL_CHANGE_DELAY) {
        delay(50);  // Debounce
        decreaseVolume();
        lastVolChangeTime = millis();
      }
      break;
    case 0xFF02FD: // Volume Up
      if (millis() - lastVolChangeTime > VOL_CHANGE_DELAY) {
        delay(50);  // Debounce
        increaseVolume();
        lastVolChangeTime = millis();
      }
      break;
  }
}

void changeScreen(const char *line1, const char *line2) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(line1);
  lcd.setCursor(0, 1);
  lcd.print(line2);
}

void displayNowPlaying() {
  if (currentSongIndex >= 0 && currentSongIndex < mp3Files.size()) {
    changeScreen("Now Playing:", mp3Files[currentSongIndex].c_str());
  }
}

void playNextMP3() {
  if (mp3Files.empty()) {
    return;
  }

  if (currentSongIndex >= 0 && currentSongIndex < mp3Files.size()) {
    currentSongIndex++;
    if (currentSongIndex >= mp3Files.size()) {
      currentSongIndex = 0;
    }
  } else {
    currentSongIndex = 0;
  }

  displayNowPlaying();
  audio.connecttoFS(SD_MMC, mp3Files[currentSongIndex].c_str());
}

void playPreviousMP3() {
  if (mp3Files.empty()) {
    return;
  }

  if (currentSongIndex >= 0 && currentSongIndex < mp3Files.size()) {
    currentSongIndex--;
    if (currentSongIndex < 0) {
      currentSongIndex = mp3Files.size() - 1;
    }
  } else {
    currentSongIndex = mp3Files.size() - 1;
  }

  displayNowPlaying();
  audio.connecttoFS(SD_MMC, mp3Files[currentSongIndex].c_str());
}

void decreaseVolume() {
  int currentVolume = audio.getVolume();
  if (currentVolume > 0) {
    audio.setVolume(currentVolume - 1);
    displayVolume(currentVolume - 1);
  }
}

void increaseVolume() {
  int currentVolume = audio.getVolume();
  if (currentVolume < 21) {
    int newVolume = currentVolume + 1;
    audio.setVolume(newVolume);
    displayVolume(newVolume);
  }
}

void displayVolume(int volume) {
  displayingVolume = true;
  volumeDisplayEndTime = millis() + DISPLAY_VOLUME_DURATION;
  changeScreen("Volume:", String(volume).c_str());
}


bool i2CAddrTest(uint8_t addr) {
  Wire.begin();
  Wire.beginTransmission(addr);
  if (Wire.endTransmission() == 0) {
    return true;
  }
  return false;
}

EDIt : found

Code: Select all

    case 0xFFA857: // Pause/Play (Use the correct IR code for your remote)
      delay(50);  // Debounce
      togglePausePlay();
      break;
      
      void togglePausePlay() {
audio.pauseResume();
}
     

Who is online

Users browsing this forum: No registered users and 69 guests