Closed mhos closed 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; }
}
}
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?
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.