earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
1.99k stars 433 forks source link

MP3 SPIFFS looping. Want playback when button pushed. Finsh until button pushed again. #607

Closed mhos closed 1 year ago

mhos commented 1 year ago

I am a tinkerer trying to put together a retirement gift for a friend of mind that plays a train whistle when a button is pushed. I want to simply press a button, play the whole MP3 then stop. If they push the button again I want it to play the full MP3 once more and so on. My problem is, I have to hold down the button for the MP3 to play. If I let go of the button it stops playing. If I press and hold again it continues where you let go of the button until it starts over. How can I make this code play the full MP3 with a single button press then stop but if you press the button again it plays it again?

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
#define switchPin D6

AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2S *out;

void setup()
{
  WiFi.forceSleepBegin();
  Serial.begin(115200);
  delay(1000);
  SPIFFS.begin();
  file = new AudioFileSourceSPIFFS("/train.mp3");
  out = new AudioOutputI2S();
  mp3 = new AudioGeneratorMP3();
  mp3->begin(file, out);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop(){
  int btn_Status = HIGH;
  btn_Status = digitalRead (switchPin);
  if (btn_Status == LOW) { 
    if (mp3->isRunning()) {
      if (!mp3->loop()) mp3->stop();
    } else {
      Serial.printf("MP3 done\n");
      delay(1000);
      { file->close(); delete file; delete mp3; }
    }
  }
}

I have a few other version that I have played around with but still cannot get it to do what I want. Again I am trying to press a button, play the whole MP3 then stop. If the button gets pressed again, play the whole MP3 then stop. Thank you all in advance. I am just a tinkerer self-taught trying to do something nice for a friend.

mhos commented 1 year ago

Figured I would post an update. I was able to figure things out. Push button mp3 plays once. Clears the file then resets. Push button it plays again like it should. Hopefully this may help others and me for later reference.

#include <ESP8266WiFi.h>
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"

AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2S *out;

int i = 1;
void setup()
{
  WiFi.forceSleepBegin();
  Serial.begin(115200);
  SPIFFS.begin();
  file = new AudioFileSourceSPIFFS("/train.mp3");
  out = new AudioOutputI2S();
  mp3 = new AudioGeneratorMP3();
  mp3->begin(file, out);
  pinMode(D6, INPUT_PULLUP);
}

void loop()
{
  if(digitalRead(D6)==0)
  {
    while(digitalRead(D6)==0)
    {delay(10);}    
    i++;
  }

  if(i%2 == 0)
  {
    if (mp3->isRunning()) {
      if (!mp3->loop()) mp3->stop();
  }
  else { file->close(); delete file; delete mp3; }
  }
}