Python:使用pygame播放声音

时间:2015-11-29 13:08:33

标签: python audio pygame psychopy

所以我试图以一种只要声音持续的声音将与图像一起播放的方式运行for循环。我可以看到图像显示很短的时间,我也听到了播放的声音。但是一切都停止了,我无法在窗口中输入内容(我应该在0.85秒内输入' j' f')。在此之后,应该开始新的试验,但它没有。因为我得到的这个错误,不知道是不是这样:

sound1 = sound.SoundPygame(value=stimulussound)

AttributeError: 'SoundPygame' object has no attribute 'SoundPygame'

我不明白为什么我得到了这个错误,因为sound1正在进行第一次试验。但是声音停止后,图像消失了,屏幕上显示了固定交叉,但是固定交叉在0.85秒后没有消失......而且如果我按下J的F,它也会将它保存在一个变量中!它还节省了反应时间! 无论如何,这是我的代码,为什么在第一次试运行之后开始第二次试用呢?

#showing instructions
instructions = visual.TextStim(window, text=actualinstructions)
instructions.draw()
window.flip()

#waiting for j-key before rest of experiment runs
if event.waitKeys("j"):
    window.flip()    
    start = True 

#whileloop to start experiment
while start == True:
#forloop
    for i in range (0,192):
    #saving the two needed pathways in a variable
        stimulusimage = conditiesalles[int(i)][int(2)]
        stimulussound = conditiesalles[int(i)][int(3)] #f.e. C:\Users\Ineke\Documents\Python Scripts\Project\stimuli\Sounds\Negative\6.wav



    #lengthofsound: using function 
        sound1 = sound.SoundPygame(value=stimulussound)
        lengthofsound = sound1.getDuration()

    #resetting timer and making a variable that knows the time 
        timerClock.reset()
        currentTime = timerClock.getTime()
        if (currentTime <= lengthofsound):
            #imagestim
            showingimage = visual.ImageStim(window, image=stimulusimage)
            showingimage.draw()
            window.flip()
            #soundPygame
            sound = sound.SoundPygame(value=stimulussound)
            sound.play()

        if (currentTime > lengthofsound):
            timerClock.reset()
            window.flip()

        if (currentTime <= timefixationcross):
            #fixatiekruis
            Fixationcross.fixationscross(window)

            #getKeys j of f = inputuser
            if event.getKeys('j'):
                inputuser = 'j'
                reactiontime = currentTime
            elif event.getKeys('f'):
                inputuser = 'f'
                reactiontime = currentTime
            else: 
                inputuser = "geen input of foute knop gebruikt"
                reactiontime = "geen reactie"
        inputsuser.append(inputuser)
        reactiontimesuser.append(reactiontime)

    #closing the window
    start == False    
    window.close()

1 个答案:

答案 0 :(得分:2)

发生此错误是因为您在此行中覆盖变量sound

sound = sound.SoundPygame(value=stimulussound)

...所以变量“sound”在运行此行后不再指向psychopy.sound模块。该错误消息告诉您没有psychopy.sound.SoundPygame.SoundPygame这样的属性。解决方案只是将上面一行中的变量重命名为与“sound”不同的内容。

作为旁注,您似乎在每次试验中创建相同的声音对象。 sound1以及您当前称之为sound的内容均为sound.SoundPygame(value=stimulussound)。为什么不只有一个?