Repeating sound component without a loop

OS (e.g. Win10): Windows 11
PsychoPy version (e.g. 1.84.x): v2023.2.3
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
I am trying to repeat a sound component infinitely with a mouse component without creating a loop (because I also have a scale component).

What did you try to make it work?:
I tried to add this code component, but it does not seem to work:

#Begin routine
sound_started = False
#Each frame
if mouse_stim1.getPressed()[0]:  # Check if the left mouse button is pressed
    if not sound_started:   # Play the sound only if it hasn't started yet
        sound_started = True
    sound_started = False  # Reset the flag when the mouse button is released

My experiment contains the following:

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.
The issue is, I don’t get any error and I sometimes feel that the code is not executing… In any case, the code is not applied.

Try putting the sound component in an instructions routine and then repeat it in code in the trial routine.

Sorry, I’m quite new to PyschoPy and I’m not sure I get what you want me to do…

Here is the file if you need it for instructions. Thank you very much!
Essai4_setrepeat.psyexp (37.7 KB)

Do you mean I need to copy/paste the sound component in an instruction routine and then retrieve the code from that sound component and insert into the trial routine?

When you use code like it can refer to an audio component from an earlier routine.

It works!! Thank you very much!!