Problem looping a sound

I’m trying to loop a sound when button is pressed, and having some trouble. Shouldn’t this block play the sound 5 times?

loop

This just plays the sound once though.

Any tips to get a sound to loop in Thunkable X?

Or to get the old Player object, that had a loop property?

Edit: I think the issue with the method in my screenshot aove is that it’s executing the Sound1.Play action 5 times instantly without waiting for the sound to finish playing… In the method below for example the text of “now playing” instantly becomes “10”, instead of iterating from 1 to 10 with each loop.

loop2

So far the best solution I’ve come up with is something like the below. This successfully loops the sound 4 times, and I could keep adding blocks to simulate an infinite loop, but that’s obviously uggggllllyyyyy and shouldn’t be necessary.

Yes.
Except it will be playing it 5 times at the same time.

The reason for this is that the sound play is just a request to the OS to activate whatever is required to decode the mp3 and play it. And the reason why the system does not wait until it is finished is because you need to be able to run sound simultaneously, like when a game will have to feature background music and sound effects.

The best way to run multiple times is probably to use a start signal, with a delay to avoid checking too often, something like:

image

(Note: this is UNTESTED. It is possible that I am way off with this and that it does not work; it is 4 AM here and I should be in bed already. But it seems like it should.)

The way this works is that the repeat loop sets a logical playing as a marker of a current activity. Asynchronously, the logic will get to the loop of waiting 1 second until the end of the play resets the value of the logical that will break-off out of the while loop, which will then fall back to the repeat loop.

If this does not work, leave a note to this effect and I will check it out when I get up.
If it works, mark it as solved.

3 Likes

Anyone figure out how to make an infinite loop of music?

Hi, @looovelee! :wave:

The repeat block might help you -

image
(I have used the ∞ block with this.)

Thanks! :blush:

1 Like

Hello Kartik! :wave:

Thank you so much for your suggestion. I never saw the option to do infinity. That sounds like a solution. :sweat_smile:

I tried a few things.

First I tried to use this only on the background guitar and then also only on the tracks, but no matter where I put this block, nothing is looping when I tested it out.

The blocks screenshot below actually makes the screen freeze after the audio tracks and background music plays 1x.

I wonder what I’ve done wrong here. :thinking:

1 Like

You just did a small mistake, it should be as -

image

Thanks! :blush:

1 Like

Thank you Kartik!

I tried this next, but my app doesn’t seem to like it. :upside_down_face:

The background music will not play, the tracks are playing sporadically and sometimes 2 or 3 are playing simultaneously instead of one after another. The pause button no longer works properly either. :anguished:

Is it possibly getting too complex with all of the functions?

  1. Background music playing
  2. Tracks being called from database
  3. Pause button

I don’t think this will work @kartik14.

If you think about it, all you’re doing here is calling the Sound1.Play function an infinite number of times - there’s nothing about waiting for the sound to finish.

Something like this might work for you @looovelee (but you should probably include some logic to stop/pause the music too)

3 Likes

This is really brilliant… Looks to me like recursion

3 Likes

In order to understand recursion, first you must understand recursion! :joy:

Thanks @kufirre

5 Likes

Hey!

I’m SO sorry it’s taken a year (life has been CRAZY, as we all know…), but I finally picked this app back up… and this works like a charm! :heart_eyes_cat:

Thank you all so much for your advice. :clap: :grin: :clap:

2 Likes

Heyyo
How would you do this in Thunkable X? The blocks are a bit different.

1 Like