Repeat the sound component only in a routine

I have a routine “practiceRating” which has an audio component and a slider. The participants hear the audio once and they can play the audio again (only one more time), then they should use the mouse to click on one option on the slider and then click on an image to m ove to the next trial.
My issue is that I couldn’t find a way to replay the sound component again if the participant chose to (i.e., by clicking on, let’s say, ‘r’ key).
note: I created an inner loop ‘repeats’, chose ‘2’ for nReps, and unchecked ‘Is trials’, added a code in the end routine, but it did
not work!
attached are the screenshots.
Thank you so much,

Correct me if I am wrong, but it looks like you tried to take the following approach:

This approach creates a loop to repeat the sound. If you press the slider, you want the loop to be finished. If the r key was pressed, you want the loop to start iterating.
As long as the participant presses the r key, the loop won’t be over. It will repeat the routine repeatedly until the participant presses the slider to finish the experiment.

Everything in your code seems to be fine except one thing. You need to set the keyboard component to end the routine as well. So, the routine will be over whenever the participant presses r, and the loop will start its iteration.


I created a simplistic example demonstrating this approach:
Repeat (20.3 KB)

Let me know if it fixed your inquiry and if you need any help :slight_smile:

Thank you for responding to my question. Unfortunately, I wasn’t able to run the example you created “Repeat Sound”. It gave an error.
I checked the code you added and modified mine in addition to checking the ‘force end of routine’ box for the ‘r’ key.

Here are all the components of the routine:

However, now when I press ‘r’ the sound is repeated, and the whole routine with the images repeats (I used images instead of words on the slider because I couldn’t use Arabic words, image_15, 16, 17, 18, 19).
The following picture is the screen that the participant gets at the beginning when he/she hears the sound for the first time.

The following pic. is for the screen when the participant chooses an option on the slider.

I want the options on the slider (i.e., images) to stay on the screen with the answer that the participant chose (red circle) and the yellow rectangle that only appears when a choice is pressed, and only the sound of that trial to repeat.

That is, if the participant decided to repeat the sound one more time, he/ she can press ‘r’ and only the sound repeats and everything else stays the same until he/she confirms the answer (or change it, then confirm) by clicking on the yellow rectangle (image_14) on the left. If this yellow rectangle was pressed, the next trial starts. So, how can I repeat only one component of the routine (i.e., the sound) without repeating the whole routine? and how can I link ‘r’ key pressing to repeating only that sound once more without moving to the next trial? because now, when I press the ‘r’ key twice (I set the nReps in repeats loop to 2) the next trial starts, and I only want the next trial with the new sound to start if the participant clicked on the choice on the slider and pressed on the image (yellow rectangle) for confirmation. The only thing that should end the ‘trials_2’ loop routine is the mouse click on the yellow rectangle and not ‘r’.
I am sorry for this long message. The experiment was in perfect shape until I decided that I need to give the participants the option to hear the sound again before they make their decision and confirm it by clicking on the yellow rectangle.

The approach you mentioned will result in the routine repeating itself from the start. Therefore your text won’t be visible at the beginning of each routine.
I would try to find another approach to repeating the sound, not the one you used.

I didn’t find a way to do it solely with PsychoPy built-in classes, but using Pygame might work here.

I didn’t test it myself, but you can try to install Pygame and do something like:

# Start Experiment:
import pygame


mySound = pygame.mixer.Sound('sound.wav')

# Each Frame:
keys = kb.getKeys()

for thisKey in keys:
    if thisKey == 'r':

I have Psychopy 2022.2.5, Windows
I got the following error

Here is the experiment @Chen (559.1 KB)

Now the experiment starts, but both pressing ‘r’ and the mouse click would end the routine. Also, the sound doesn’t repeat.
Note: I don’t want the same sound to be repeated, instead the sound of that specific trial to be repeated ($path) in the condition file. (604.9 KB)

I figured it out.

What was the solution?