Python:使用pygame播放声音
所以我试着运行一个for循环,让声音和图像一起播放,直到声音持续。我可以看到图像显示很短的时间,而且我还听到声音播放。但随后一切都停止了,我无法在0.85秒内在窗口中给出输入(我应该用“j”或“f”)。在这之后,一个新的试验应该开始,但它没有。不知道是不是因为这个错误,我得到:Python:使用pygame播放声音,python,audio,pygame,psychopy,Python,Audio,Pygame,Psychopy,所以我试着运行一个for循环,让声音和图像一起播放,直到声音持续。我可以看到图像显示很短的时间,而且我还听到声音播放。但随后一切都停止了,我无法在0.85秒内在窗口中给出输入(我应该用“j”或“f”)。在这之后,一个新的试验应该开始,但它没有。不知道是不是因为这个错误,我得到: sound1 = sound.SoundPygame(value=stimulussound) AttributeError: 'SoundPygame' object has no attribute 'SoundP
sound1 = sound.SoundPygame(value=stimulussound)
AttributeError: 'SoundPygame' object has no attribute 'SoundPygame'
我不明白为什么我会犯这样的错误,因为sound1是在第一次试听时播放的。但声音停止后,图像消失,屏幕上会显示fixationcross,但fixationcross在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()
#显示说明
指令=visual.textsim(窗口,文本=实现指令)
说明.绘图()
window.flip()
#在剩下的实验运行之前等待j键
如果事件等待键(“j”):
window.flip()
开始=真
#whileloop开始实验
当start==True时:
#前环
对于范围(0192)内的i:
#将两条需要的路径保存在一个变量中
刺激值=条件值[int(i)][int(2)]
stimulussound=conditieSales[int(i)][int(3)]#f.e.C:\Users\Ineke\Documents\Python Scripts\Project\stimuli\Sounds\Negative\6.wav
#lengthofsound:使用函数
sound1=声音。SoundPygame(值=刺激声)
lengthofsound=sound1.getDuration()
#重置计时器并生成知道时间的变量
timerClock.reset()
currentTime=TimerLock.getTime()
如果(currentTime lengthofsound):
timerClock.reset()
window.flip()
如果(currentTime发生此错误是因为您覆盖了此行中的变量sound
:
sound = sound.SoundPygame(value=stimulussound)
…因此变量“sound”在这一行运行后,它不再指向Psycopy.sound
模块。错误消息告诉您没有Psycopy.sound.SoundPygame.SoundPygame
这样的属性。解决方法只是将上面一行中的变量重命名为与“sound”不同的名称
作为旁注,您似乎在每次试用中都创建了相同的声音对象。sound1
和您当前所称的sound
都是sound.SoundPygame(value=stimulussound)
。为什么不只使用一个呢?好吧,我现在知道哪里出了问题。现在不要再出错了。我还监督了我有sound1和sound的事实;我现在只使用sound1。但是当我现在尝试运行while循环时,我创建的窗口只是冻结,不再对任何东西做出反应。无法使用window关闭它。close()也可以。你可能知道如何解决这个问题吗?谢谢!是的,那是因为倒数第二行应该有一个等号而不是一个双等号,即给start
赋值,而不是进行比较。现在while
循环只是永远保持循环,因为start
永远是真的。