Play audio multiple times in program

Hi,

I'm trying to run example sketch from ESP8266 audio library. The main task of the program is to play audio from array in which data of audio is stored in hex format. The code runs successfully without any issue but the audio plays only once at startup. If I want to play another time, then I have to flash/restart the ESP32. I want the audio to be played to be played repeatedly or multiple times without restarting the ESP32.
How do I edit below code?


 #include <Arduino.h>

#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2S.h"

// VIOLA sample taken from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
#include "viola.h"

AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2S *out;

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.printf("WAV start\n");

  audioLogger = &Serial;
  file = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
  out = new AudioOutputI2S();
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);
}

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

Also, can anyone please explain what's happening in void loop() ?

Try moving some of the the last 4 four lines from the void setup to the void loop. I have never used this library before but I'm pretty sure it declares the file in one of these lines

file = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
  out = new AudioOutputI2S();
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);

and then just lets it play out in void loop.

I've never used that library but apparently...

wav->begin(file, out);
Starts playback as part of setup(), which runs once before the main loop() starts.

Then your main loop() runs forever but the if-conditions prevent it from doing anything "useful" after playback is stopped.

Just in general, loops... ** for-loops, while() loops and do-while loops that do something over-and-over (usually until some condition is reached) are one of the two most important concepts in programming. i.e. If you want to play the sound 20 times and then stop, you loop 20 times.

If you just want to do the same thing twice, It's sometimes easier to write the same code twice, or to write a function and call it twice.

In your case it's a little tricky because you need to know the audio is done playing before you start it again.

The other important concept is conditional execution, mostly if-statements. This is how computers "make decisions".

@DVDdoug Thank you so much for giving your time to explain in detail. I do have basic knowledge about programming. The thing I didn't understand in void loop() part is the meaning of 'if' condition. What is the condition doing in that part. Audio doesn't play if I remove the code in void loop() even if wav->begin(file, out) is present in code. So it says that playing Audio is dependent on that part of code. I just don't get the logic of code in void loop() part.

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