ESP8266 play two mp3 files one after the other

Hi my friends,

I did several useless tests. I tried duplicating the objects (one for each mp3 file). Did not work.

I tried several other random things, to no avail.

I have two mp3 files (10Kb each) recorded in SPIFFS.h of a nodemcu 12E 1.0

The code below correctly reproduces 1 file

But I need the second file to also be played right after the first (difference of a few milliseconds)

The second file is called (filetwo.mp3)

The loop ends after playing the second file

Does anyone know what this code should look like for this to happen ?

#include <Arduino.h>
#include <FS.h>
#include <ESP8266WiFi.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2SNoDAC.h"

AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2SNoDAC *out;
AudioFileSourceID3 *id3;


// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.)
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
  (void)cbData;
  Serial.printf("ID3 callback for: %s = '", type);

  if (isUnicode) {
    string += 2;
  }
  
  while (*string) {
    char a = *(string++);
    if (isUnicode) {
      string++;
    }
    Serial.printf("%c", a);
  }
  Serial.printf("'\n");
  Serial.flush();
}


void setup()
{
  WiFi.mode(WIFI_OFF); 
  delay(2000);
  Serial.begin(115200);
  
  if(!SPIFFS.begin()) {
    Serial.println ("An Error has occurred while mounting SPIFFS");
    return;
  }

  Dir dir = SPIFFS.openDir("");
  while (dir.next()) {
    Serial.print(dir.fileName());Serial.print(" ");Serial.println(dir.fileSize());
  }

  delay(2000);
  
  out->SetGain(3.00);
 audioLogger = &Serial;
  file = new AudioFileSourceSPIFFS("/fileone.mp3");
  id3 = new AudioFileSourceID3(file);
  id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
  out = new AudioOutputI2SNoDAC();
  mp3 = new AudioGeneratorMP3();
  mp3->begin(id3, out);

}

void loop()  {
    
  if (mp3->isRunning()) {
    if (!mp3->loop()) mp3->stop();
  } else {
      Serial.printf("MP3 done\n");
      delay(1000);
    }

}

Thanks

Please set the output gain only once in setup().

load the second file in your if-statement in loop() - and keep the state so that you know "this is the second and I'll stop after it".

Done

The second part is what I tried in the last few hours, and I couldn't

I was able to listen to two or more mp3 audios in sequence.

At least it works like this:

#include <Arduino.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2SNoDAC.h"

AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2SNoDAC *out;
AudioFileSourceID3 *id3;

int terminou;
int tocasom;

void setup()
{
  delay(2000);
  Serial.begin(115200);
  Serial.println();
  
  if(!SPIFFS.begin()) {
    Serial.println ("An Error has occurred while mounting SPIFFS");
    return;
  }

  Dir dir = SPIFFS.openDir("");
  while (dir.next()) {
    Serial.print(dir.fileName());Serial.print(" ");Serial.println(dir.fileSize());
  }
    
  id3 = new AudioFileSourceID3(file);  
  out = new AudioOutputI2SNoDAC();
  out->SetGain(3.00);
  mp3 = new AudioGeneratorMP3();

}
//------------------------------------------------------------------------------------------
void loop() {

  if(terminou == 0 && tocasom == 0) {
    mp3->begin(new AudioFileSourceSPIFFS("/sound0.mp3"), out);
    tocasom = 1; 
  }
  if(terminou == 1 && tocasom == 1) {
    mp3->begin(new AudioFileSourceSPIFFS("/sound1.mp3"), out);
    tocasom = 2;
    terminou = 0;
  }
  if(terminou == 1 && tocasom == 2) {
    mp3->begin(new AudioFileSourceSPIFFS("/sound2.mp3"), out);
    tocasom = 3;
    terminou = 0; 
  }
  
  if(!mp3->loop()) { 
    mp3->stop();
    terminou = 1;
  } 
  
}

I'm going to go further to see if I can hear a String with 1 letter and 3 numbers. There will be 4 mp3 audios said in sequence. For example: G564.

G-5-6-4 on the audio output. I have no idea how I'm going to do this, but I'm going to try.

I think about dividing the String into substring and including expanding this code. I will need to have 11 mp3 files recorded in SPIFFS (LITTLEFS). The letter + numbers from 0 to 9.

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