How to use code component to play audio at t==1?

Hi There,

You are very close! But there are a few things to take into account here.

Firstly, t is a float value, and we cannot/shouldn’t test floats for equality in python (because they will never be equal) for this reason we may need to use the >= operator rather than ==.

Secondly, you might want to instead use a clock that restarts at the begining of every trial so replace t with something like nameofroutineClock.getTime().

Third, we only want to initiate the presentation of a sound on a single frame, rather than trying to call .play() on several frames.

So, in your code component you want something like this in your begin experiment/routine tab:

mySound = sound.Sound('folder/spring1.wav.wav')
mySound.started = False
presentationTime = 1

Then in your ‘every frame’ tab use something like:

if myTrialClock.getTime()>1 and mySound.started==False:
    mySound.play()
    mySound.started=True

Then in the end routine tab:
mySound.started=False

Hope this helps,
Becca