Sound file cannot be played in a loop

Hello, the problem I am currently experiencing is that my sound file cannot be played in a loop

Can someone help me? This is really important to me, thank you for your help!

I have read all the articles about sound loops or music loops in the forum, but still can’t solve my problem.
What I want to achieve is to repeat a sound 70 times during the experiment.

I wrote loops = 70 in my code , but only played once during the actual operation, and never played again later. I now want to achieve 70 repeats of playback under this code structure. I don’t know if it can be achieved.

My previous idea was to write a block that plays sound stimulation in the gui version, and then loop the block 70 times, but there are loops in my own code, I am worried that the nested loops in the loop will not be good.

This is my code:
numReps = 70
bstim = sound.Sound(value=bsoundfilename,loops=numReps-1,sampleRate=11025,autoLog=True)
dstim = sound.Sound(value=dsoundfilename,loops=numReps-1,sampleRate=11025, autoLog=True)
random.shuffle(adaptrials)

for trial in adaptrials: # 1 set of adaptors + test
    
    while paused:
        waitforunpause()
    
    # LISTEN prompt
    listenScr.draw() #draw test indicator slightly before soundfile
    win.flip()        
    
    i+=1
    
    if trial[0]==bendpoint:
        cond='b'
        adaptor = bstim
    else:
        cond='d'
        adaptor = dstim
        
    trialClock.reset()
    
    while trialClock.getTime() < adapGap:
        checkEsc()
        
    # play adaptors
    
    for j in range(trial[1]): # number of repeats
        trialClock.reset()  # clock
        adaptor.play()
    
    trialClock.reset()
    adaptor.play()
    
    while trialClock.getTime() < expoDur*numReps+2:
        longcheckEsc(adaptor,trialClock)
            
    # switch from LISTEN to RESPOND
    win.flip(clearBuffer=True) # clear window            
    trialClock.reset()
    while trialClock.getTime() < adapGap*.5:
        checkEsc()

My computer version is macOS 10.14.6
My psychopy version is 2020.2.4post1(install by conda)

I would just put all of the code for playing the sound file once in a for loop, for example:

for rep in range(numReps):

Or, if you’re using the Builder view, you could put it in a Loop in the Flow panel with nReps set to 70, which would achieve a similar effect

Do you mean I should do this?

if trial[0]==bendpoint:
    cond='b'
    adaptor = bstim
else:
    cond='d'
    adaptor = dstim

to

    if trial[0]==bendpoint:
        cond='b'
            for rep in range(numReps):
              adaptor = bstim
    else:
        cond='d'
            for rep in range(numReps):
                adaptor = dstim

This is all of my code :

for rep in range(4): # number of repetitions of (adaptors+test) * 2 endpoint adaptors
numReps = 70

bsoundfilename = _thisDir + os.sep + "soundfiles/" + bendpoint
bstim = sound.Sound(value=bsoundfilename,loops=numReps-1,sampleRate=11025,autoLog=True)
dsoundfilename = _thisDir + os.sep + "soundfiles/" + dendpoint
dstim = sound.Sound(value=dsoundfilename,loops=numReps-1,sampleRate=11025, autoLog=True)

random.shuffle(adaptrials)

for trial in adaptrials: # 1 set of adaptors + test
    
    while paused:
        waitforunpause()
    
    # LISTEN prompt
    listenScr.draw() #draw test indicator slightly before soundfile
    win.flip()        
    
    i+=1
    
    if trial[0]==bendpoint:
        cond='b'
            for rep in range(numReps):
              adaptor = bstim
    else:
        cond='d'
            for rep in range(numReps):
                adaptor = dstim
        
    trialClock.reset()
    
    while trialClock.getTime() < adapGap:
        checkEsc()
        
    # play adaptors
    
    #for j in range(trial[1]): # number of repeats
    #    trialClock.reset()  # clock 
    #    adaptor.play()
    
    trialClock.reset()
    adaptor.play()

I don’t think that would work as then you’d simply be setting the sound 70 times, not playing it. I’ve just seen the loop for j in range(trial[1]) - this looks to be in the right place, but what is the value of trial[1]? Is it the number of repeats? Where is this set?

I understand that my previous thinking was wrong.
You said that trial[1] of for j in range(trial[1]) is not defined in my code, but I tried to give it an integer, such as 10 (for j in range(10)).
But it can still only be played once.
Should I rewrite a looping code?
What should I do?

Could it be that the script is not pausing execution while the video is playing? You could try adding this after adaptor.play():

while adaptor.status == PLAYING:
    pass

and adding to the beginning of the code:

from psychopy.constants import FINISHED, NOT_STARTED, PAUSED, PLAYING, STOPPED

Thank you very much for your reply!
But it seems that the problem still exists and has not been resolved.

I wrote the following code as you said
for j in range(7): # number of repeats
trialClock.reset() # clock
adaptor.play()
while adaptor.status == PLAYING:
pass

And I deleted the code (loops=numReps-1)
bsoundfilename = _thisDir + os.sep + “soundfiles/” + bendpoint
bstim = sound.Sound(value=bsoundfilename,sampleRate=11025,autoLog=True)
dsoundfilename = _thisDir + os.sep + “soundfiles/” + dendpoint
dstim = sound.Sound(value=dsoundfilename,sampleRate=11025, autoLog=True)

But this seems to only be repeated a few times (I tried 7 times), but it won’t work if the number of repetitions is too large (I was 70 times)
And the sound played is not my voice stimulation itself, but noise, my voice is simple ba and da sounds.

This is all my code (about this part)

for rep in range(4): # number of repetitions of (adaptors+test) * 2 endpoint adaptors

bsoundfilename = _thisDir + os.sep + "soundfiles/" + bendpoint
bstim = sound.Sound(value=bsoundfilename,sampleRate=11025,autoLog=True)
dsoundfilename = _thisDir + os.sep + "soundfiles/" + dendpoint
dstim = sound.Sound(value=dsoundfilename,sampleRate=11025, autoLog=True)

random.shuffle(adaptrials)

for trial in adaptrials: # 1 set of adaptors + test
    
    while paused:
        waitforunpause()
    
    # LISTEN prompt
    listenScr.draw() #draw test indicator slightly before soundfile
    win.flip()        
    
    i+=1
    
    if trial[0]==bendpoint:
        cond='b'
        adaptor = bstim
    else:
        cond='d'
        adaptor = dstim
        
    trialClock.reset()
    
    while trialClock.getTime() < adapGap:
        checkEsc()
        
    # play adaptors
    
    for j in range(7): # number of repeats
        trialClock.reset()  # clock
        adaptor.play()
        while adaptor.status == PLAYING:
            pass
    
    #trialClock.reset()
    #adaptor.play()
    
    while trialClock.getTime() < expoDur*numReps+2:
        longcheckEsc(adaptor,trialClock)

I’m sorry to bother you, but the problem is still not resolved.
Is there any suitable solution?

Sorry, this one passed me by! I think I’d need to see how adaptors is defined to get an idea of what’s going on… Could you zip up the .py file with all the stimuli used so I can try running it on my own pc?

Okay, let me ask the teacher if I can share the complete code, because this is a project in cooperation with another lab, and the code and files belong to them.
Thank you very much for your reply, I have tried many methods, but it still doesn’t work.
I want to ask maybe using background sound is a good solution for repeating the sound?