Stopping a Piezo speaker from playing a lengthy tune

So I have a bit of a programming problem here.

I'm attempting to make a sort of jukebox if you will with a piezo speaker. I won't get into too much detail because it's not all important to my problem.

Anyways, I'm attempting to use a simple button to turn a song on and off. The idea is when I press the button, my desired song plays, and when I then press it again, it should turn off. Playing the song was simple enough, I just detected when there was a change in my buttonState and then called the function that contains the instructions for the song.

My problem is that it keeps playing the song, and I can't figure out a way to actively check the button state so that I can detect when it changes again and then break out of that function and return to my main loop() function, which will thus turn the song off.

The only way I could think of is to make a sort of button check function and call it after every tone played, but this would be both extremely tedious and inefficient. It would also only work if the button is pressed in between tones, and will not do anything if the button is pressed in the middle of a tone, rendering the button kind of clunky and useless.

Any ideas? Thank you in advance for your responses!

Check out this link. Demonstration code for several things at one time

Your problem is that the code you are using to play the tune is stuck playing the tune. I bet you made use of the delay function. Why wait in a delay when you could let your loop continue to run until it is time for the next note? That link will explain how to write non-blocking code.