Question about AudioPlaySdWav.isPlaying() and delay

Status
Not open for further replies.

nlecaude

Well-known member
Hello,

While doing some tests to play a wave file in a loop, I noticed that if I don't use a delay in my code it doesn't seem to work. Here the basic code that work:

Here's the code that work:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>

AudioPlaySdWav     wav;
AudioOutputI2S     dac;

AudioConnection c1(wav, 0, dac, 0);
AudioConnection c2(wav, 1, dac, 1);

AudioControlSGTL5000 audioShield;
void setup() {
  AudioMemory(5);

  audioShield.enable();
  audioShield.volume(20);

  SPI.setMOSI(7);
  SPI.setSCK(14);
  if (SD.begin(10)) {
    wav.play("01_16M.WAV");
  }
}

void loop() {
  float vol = analogRead(15);
  vol = vol / 10.24;
  audioShield.volume(vol);
  delay(20);
  if (!wav.isPlaying())
  {
    wav.play("01_16M.WAV");
  }
}

If I remove the delay(20) in loop() it stops working and I was wondering why... is it because it takes a while after the wav file has begun to play for isPlaying() to return true ?
 
I've noticed that too. I can't figure out how it happens but it appears that immediately after you call wav.play, isPlaying is false.
You can wait for isPlaying to be true after each wav.play. e.g.:
Code:
  if (SD.begin(10)) {
    wav.play("01_16M.WAV");
    // Wait for the wav to start playing
    while(!wav.isPlaying());
  }

Pete
 
Looks like the clicking was caused by my sd card.
What's strange is that it will loop 2 times but not 3. The third time isPlaying(); reports that it's playing but no sound comes from the Teensy...
 
Status
Not open for further replies.
Back
Top