Background music in different loops

Hello guys!

I’m using psychopy version 2021.2.3 and I’m trying to put some random background music, in ten different routines to play continuously for 30 seconds while a block of ten words is presented on the screen, in which each word is presented at random, for 3 seconds.

I tried using an excel file, to select random songs, but it didn’t work, so I used the code component in each loop with the following code. However, it only works in the first loop, in the others the background music does not play.

Insert a code component into the routine where the sound will be played. On the “Start Experiment” tab, enter code like this to create the sound object in advance:

background_sound = sound.Sound(‘your_sound_file.wav’)
In the “Start routine” tab, put something like this, so that the sound starts playing, but only on the first iteration:

if your_loop_name.thisN == 0:
background_sound.play()

On the “End Routine” tab:

if your_loop_name.thisN == 10:
background_sound.stop()

Pardon my English. I’m new to Psychopy and I don’t understand programming, can anyone help me?

I understand you want to play music according to different conditions, so you can set it according to the conditions

What I do if I want to play a sound across multiple routines is.

Create the sound in a sound component, e.g. background_sound, with zero volume (online this only works if you use a sound file rather than a frequency) and the file set to update each repeat.

Then use the code you mentioned but you also need

background_sound.setVolume(1)

Hello, Wen!
Thank you for your comment. In block_A_1 the code and background music works perfectly, but when I configure block_A_2 with the same code, same audio excel file in trials to play a random song from the file, the background music does not play. I don’t know why. Thanks for your contribution!
Pardon my English. Do you know what I might be doing wrong in the experiment?

Hi! Thanks for your comment! I tried to apply your suggestion, but it still didn’t work.
In block_A_1 the code and background music work perfectly, but when I configure block_A_2 with the same code, same excel audio file in attempts to play a random music from the file, the background music does not play. I do not know why. Thanks for your contribution!

Pardon my English. Do you know what I might be doing wrong in the experiment?