2nd Sound does not play in a “when timer fires”, but 1st sound plays when a button is pushed

For school we had to make a Happy New Year countdown timer, and it was required to have a sound (sndCount) during the countdown (when btnStart is clicked), and when the timer reaches 0, we stop that first sound and play the second sound (sndHappyNewYear). I have moved the “play sound” for the second one in different spots, and it only plays if I put it under the “when btnStart clicked”. How do I make “sndHappyNewYear” play when the countdown reaches 0? Also, yes, I checked the volume, rechecked the file was uploaded, I tried adding a delay after the first sound is stopped and the second one plays, and I even tried making a separate “if” statement for when the countdown is 10, it plays the first sound. I tried that to see if I put BOTH sounds under the “when timer fires” whether they work or not, but they both did not work. Thanks!

The problem is that the Sound PLAY blocks are asynchronous (ie code execution continues without waiting for the action to complete) , as indicated by the “Then Do” sections. When you call muliple asynchronous blocks without waiting for then to complete, error often occur and in unpredictable patterns.

There are several ways to try to address this issue. The simpliest for your example is to nest one call within the other’s THEN DO section. Another more complicated technique is to create a function that waits until the asynchronous operation is finished. You can read about my approach to the problem herer Wrapping Asynchronous (then do) blocks into a Synchronous Function resolves unpredictable results.

You can also throw wait blocks in there. The problem I have found is that that the amount of time required for the wait varies by the device and what the device has going on at the time. It can reduce the errors, but unless you use a REALLY long wait (10 seconds), they will still occur from time to time.

Happy Thunking!

1 Like