Trouble making code play WAV file on repeat with pushbutton

Hi there. I’m trying to make a push button start a WAV file and play it on repeat. Right now, the push button plays the audio but only once. Without the push button, the code allows the audio to play on repeat. Does anyone know how I can make the push button play the audio continuously?

#include <SD.h>
#include <TMRpcm.h> 
TMRpcm tmrpcm;

int wasPlaying = 0;
int finished = 0;
int start = 0;
int buttonPin = 2; //digital pin where the button is
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  Serial.print("\nInitializing SD card...");
  pinMode(10, OUTPUT); 
  if (!SD.begin(10)) {                                  
    Serial.println("failed!");
    return;
  }
  Serial.println("done.");
  tmrpcm.speakerPin = 9; //digital pin where the speaker is
}

void loop() {
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);

  if(buttonState == HIGH){ //if button is pressed, test.wav is played once
  start = 1;
  playNext();
  if(tmrpcm.isPlaying())
    while(tmrpcm.isPlaying());
  tmrpcm.stopPlayback();
  }
}

void playNext() { 
  if(wasPlaying == 1) {
    Serial.println("Completed playback.");
    wasPlaying = 0;
    finished = 1;
    start = 0;
  }
  tmrpcm.play("test.WAV");           
  wasPlaying = 1;  
}

Do you mean you press the button once, briefly, and then the wav starts playing over and over? Or do you mean the wav plays over and over as long as you hold the button down?

Hi, I mean press the button once, briefly, and then the wav starts playing over and over. Thank you for your quick response.

void loop() {
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);

  if(buttonState == HIGH){ //if button is pressed, test.wav is played once
  start = 1;
  playNext();
  if(tmrpcm.isPlaying())
    while(tmrpcm.isPlaying());
  tmrpcm.stopPlayback();
  }
}

Can you spot the line that starts the song now? Can you spot the end of the song in your code now? Why not just put it in a while loop and let it keep running that bit of code over and over?

What should stop it from repeating? Or should it just go forever and ever?

kaylavalerie5: I'm sorry, I'm very new to arduino. I took parts of codes from examples to make things work. So I don't quite understand what you are saying.

So your first step should now be to go read up on the libraries you are using and learn how the code you have works. Trying to muddle your way through with zero understanding of what is going on is a recipe for frustration. Spend a little time reading and learning and this might make a lot more sense.

I misunderstood what you said.. now I have made it work. Thank you.