繁体   English   中英

Python:使用pygame播放声音

[英]Python: using pygame to play sounds

因此,我正在尝试运行for循环,以便在声音持续的同时将声音与图像一起播放。 我可以看到图像显示的时间很短,而且听到声音在播放。 但随后一切都停止了,我无法在0.85秒内在窗口中输入内容(我应该输入“ j”或“ f”)。 在此之后,应该开始新的试验,但是没有。 不知道那是否是因为我得到的这个错误:

sound1 = sound.SoundPygame(value=stimulussound)

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

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

#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()

发生此错误是因为您覆盖了以下行中的可变sound

sound = sound.SoundPygame(value=stimulussound)

...因此,一旦运行此行,变量“ sound”就不再指向psychopy.sound模块。 该错误消息告诉您,没有诸如psychopy.sound.SoundPygame.SoundPygame类的属性。 解决的方法就是将上述行中的变量重命名为不同于“声音”的名称。

附带说明一下,您似乎在每个试验中都创建了相同的声音对象。 sound1和您当前所说的sound都是sound.SoundPygame(value=stimulussound) 为什么不只有一个?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM