Loop medias do not change

If this template helps then use it. If not then just delete and start from scratch.

Mac Tahoe
PsychoPy 2026.1.3
Standard Standalone Installation? (y)
URL of experiment: localhost
Do you want it to also run locally? (y)
I have a loop including three routines. In the first three variables are assigned values like ‘sound1=voice1.mp3’, the second has a sound component named sound1_go playing $sound1, and the third plays $sound_2 and $video_1. The values change in every loop. The program runs smooth locally. when I deploy it online, with all media files preloaded, the routines play the same three media in every loop. They do not change. the logs shows that the loop advances but the variables like ‘sound1_go.sound’ keeps ‘undefined’, while ‘sound1’ changes as assigned from the first loop. I return to local program and find, even the program run correct but the ‘sound1_go.sound’ keeps ‘A’. I use stop() and setSound() in the begin routine tab, these problems persist. Please help me make it run correct.

Are you using code components when you could just be using sound components?

Thank for your reply! I have complicated rules to follow, so these medias cannot be passed through loop itself.

You can still set the sound file via a sound component, without the value coming from a spreadsheet. However, the $ is only relevant if you do this. Please could you show exactly how you are setting the sound?

1st routine

clpv_this=clpv[pos]
scene_this=scenes[pos]

has_visual=True
has_voice=True

if clpv_this==“0pni”:
voice_context=“media/hongsaystoming.mp3”
mov_target=“media/”+scene_this+“I.mp4”
voice_target=“media/”+scene_this+“PN.mp3”
elif clpv_this==“0pnp”:
voice_context=“media/hongsaystoming.mp3”
mov_target=“media/”+scene_this+“P.mp4”
voice_target=“media/”+scene_this+“PN.mp3”
elif clpv_this==“nncc”:#C=CPV
voice_context=“media/”+scene_this+“N.mp3”
mov_target=“media/”+scene_this+“C.mp4”
voice_target=“media/”+scene_this+“NC.mp3”
elif clpv_this==“nnnc”:#C=xNx
voice_context=“media/”+scene_this+“N.mp3”
mov_target=“media/”+scene_this+“C.mp4”
voice_target=“media/”+scene_this+“NN.mp3”
elif clpv_this==“npii”:#I=CPV
voice_context=“media/”+scene_this+“N.mp3”
mov_target=“media/”+scene_this+“I.mp4”
voice_target=“media/”+scene_this+“PI.mp3”

…about 3000 lines, the media files in every loops are subject to change by the participants’ answers, the value are stored in clpv[] and scene[], in some occasions has_visual or has_sound is set as ‘False’

2nd routine

a sound component only, plays voice_context

start 0.1 /end empty /sound $voice_context = every repeat (gray)

3rd routine

a sound and a movie component play at the same time

start $has_voice /end empty /sound $voice_target = every repeat (gray)

start $has_visual /end empty /sound $movie_target = every repeat ( NOT gray)

following with a series of other routines I think don’t matter

I use object.sound and setSound() only for debug

That looks fine. Try some print statements to check the sound variables just before you use them

I added codes to 2nd routine in begin routine tab, and used print(voice_context) and so on, found these variables were correctly passed from cplv[] and scenes[]; at the same place I used print(2nd_routine_sound.sound) got all ‘A’ locally but the sound was played as designed in every loop; while online, in browser console I got 2nd_routine_sound.sound as ‘undefined’ and in every loop the program kept playing the first sound, i.e., the first set of sounds and video were played throughout the loops. the loop counts check was ok.

When debugging, I also tried to add 2nd_routine_sound.stop() at the begin routine tab and used setSound(), nothing changed, either locally or online.

I am extremely confused with the internal mechanism of the sound component.

Thanks for your reply!

I also added print(2nd_routine_sound.sound) in each frame and end routine, all ‘A’. it seems the soundPTB object behind sound component does not change throughout the routine. I tried to find some method like refresh() but failed.

If you have the code component above the sound component and check the value of the variable in Begin Routine then the sound component won’t yet have been set to the value, which is probably why you’re seeing it as the default. How about checking it in Each Frame code, e.g.

if frameN == 5:
     print("2nd_routine_sound.sound",2nd_routine_sound.sound)

yes I DID add in begin routine, each frame, and end routine, all ‘A’.

For debug, I tried in begin routine:

2nd_routine_sound.sound=voice_context

2nd_routine_sound.preload=True

Locally the values are correct, as they were designed; but

online they shows the first value for every loop, NOT ‘undefined’, but medias still play the first set.

A little forward, though.