| Reference | Downloads | Github

Builder question - how to repeat the audio stimuli when pressed 'r' key?

OS (e.g. Win10): 10.13.4
PsychoPy version (e.g. 1.84.x): 1.85.4
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

Hi! I’m very new to the psychopy and python. this is my first time working with experiment programming, so I would love to get some advice from you guys…

I’m trying to make the audio stimuli repeat when pressed ‘r’ key.
After the audio stimulus is presented, I want to make

  • if pressed ‘return’, move on to the next task (evaluation)
  • if pressed ‘r’, listen to the stimulus again

this is what I have right now.

I basically googled for three days if I can figure out what I can do with the ‘custom code’ within the builder, but I basically have no idea where to start…
is this something I can’t do on the builder view?

Please help… please…


Basically you just want to put an infinite loop around your sound_1 routine, which only ends if the enter key is pushed.

So insert a loop just around that routine (i.e. it will be nested within your main outer trials loop):

  • don’t connect it to a conditions file (as it doesn’t need to know any information),
  • uncheck the is trials setting (as it is running within a trial), and
  • give it a large number for nReps (e.g. 99), to give enough scope for repeating as required.

Insert a code component on the sound_1 routine, and in the end routine tab, put something like this:

if 'return' in key_resp_3.keys:
    your_loop_name.finished = True # use the actual loop name here

Thank you so much for the reply!

I followed your instructions, but my problem here is that the sound file I have is only 3~4 seconds, so I don’t know if this is my soundfile problem but it just stops playing after one loop.

So I want to make it replay only once after the original trial if the participant wants to listen to the stimuli again…

is there a way to make the audio file replay only once after the trial???


Yes, just set the nReps value to 2. That way, there can only be one replay, as the loop will finish after the first replay regardless of what key the person presses.

It’s not clear why that would be. Could you provide new screenshots of your flow panel (showing the new loop), the new loop’s dialog box, and the code in the code component?

This is what I have right now. did I make a mistake anywhere?

oh, and I thought my problem was ‘force end of the routine’ so I unchecked that box, but
this was what I got from the error…

Your keyboard component should be set up as it was originally: you need a keypress to end the routine (or else the loop can neither end nor go on to its second repetition), and you need the two options ('return' and something else).

The loop is in the right place, but please show us the loop dialog box and the code in the code component.

I see I see…
So I should leave the keyboard component as it was from the beginning… got it!

so ‘return’ will be the replay, and ‘space’ will be moving on to the response.

This is what I have right now. is this the loop dialog box??

I just went with the original request:

Whatever value you put in the code should be the one that ends the loop and goes on to the next stage. So with that code, 'return' will end the loop without replaying, the other response will allow the next replay to occur.

I’ve just noticed from that last screenshot that you are using an old version of PsychoPy. It can be worth using the latest version when developing a new study, so that you’re up to date with what other people making suggestions on the forum are using (but certainly resist updating during your data gathering).

Ohh I see.

Uah, I just updated my psychopy and now I’m getting this kind of error…

I did change the monitor size, but still…

Is there anything I need to do after updating the version of psychopy?

Nevermind. I got the 1.90.1 version and now it works normally (the latest one was pretty unstable…)

But yeah. Still when I press ‘return’, the stimuli replays…

OK, something strange is happening. Please insert this code before the suggested code above:

print('Trial: ' + str(loop.thisN))
print('return' in key_resp_3.keys)

and tell us what appears in the PsychoPy output window (where you saw those error messages).