psychopy.org | Reference | Downloads | Github

Sound on keypress only plays the first time

So for some reason whenever I try to play a sound with Psychopy3 on a given keypress, I can only play the sound once – the first time. I know that the key press event is being fired correctly, because my code does all the things associated with that event except for playing the sound (after the first time). Here’s some example code of what I’m doing:

from psychopy.sound import Sound

# Window and stimulus setup omitted 

cSound = Sound('C', secs=0.1)
aSound = Sound('A', secs=0.1)
numFrames = 1000

for frame in range(numFrames):
    for key in event.getKeys():
        if key in ['backslash', 'tab']:
            if key == 'backslash':
                # do some things...
                cSound.play()

            elif key == 'tab':
                # do some other things...
                aSound.play()
    win.flip()

Any ideas why the sounds aren’t playing more than once?

I was able to solve the problem by having pygame itself handle my sounds. So I do the following now:

import pygame.mixer
from pygame.mixer import Sound

pygame.mixer.init()  # necessary to make this work
firstSound = Sound("firstSound.wav")
secondSound = Sound("secondSound.wav")

# Do the rest of the things