Python:使用pygame播放声音
Python: using pygame to play sounds
所以我正在尝试 运行 一个 for 循环,只要声音持续,声音就会与图像一起播放。我可以看到显示很短时间的图像,也可以听到播放的声音。但随后一切都停止了,我无法在 0.85 秒内输入 window(我应该输入 'j' 或 'f')。在此之后,一个新的试验应该开始,但它没有。不知道那是不是因为我收到这个错误:
sound1 = sound.SoundPygame(value=stimulussound)
AttributeError: 'SoundPygame' object has no attribute 'SoundPygame'
我不明白为什么我会收到这个错误,因为 sound1 是第一次播放。但是在声音停止后,图像消失了,屏幕上显示了 fixationcross,但是 fixationcross 在 0.85 秒后没有消失......而且如果我按 F 的 J,它确实将它保存在一个变量中!它还可以节省反应时间!
无论如何,这是我的代码,为什么在第一次试用后不开始第二次试用 运行ning 很棒?
#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.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
这样的属性。解决方案只是将上一行中的变量重命名为不同于 "sound".
的名称
附带说明一下,您似乎在每次试验中都创建了相同的声音对象。 sound1
和你现在所说的sound
都是sound.SoundPygame(value=stimulussound)
。为什么不只拥有一个?
所以我正在尝试 运行 一个 for 循环,只要声音持续,声音就会与图像一起播放。我可以看到显示很短时间的图像,也可以听到播放的声音。但随后一切都停止了,我无法在 0.85 秒内输入 window(我应该输入 'j' 或 'f')。在此之后,一个新的试验应该开始,但它没有。不知道那是不是因为我收到这个错误:
sound1 = sound.SoundPygame(value=stimulussound)
AttributeError: 'SoundPygame' object has no attribute 'SoundPygame'
我不明白为什么我会收到这个错误,因为 sound1 是第一次播放。但是在声音停止后,图像消失了,屏幕上显示了 fixationcross,但是 fixationcross 在 0.85 秒后没有消失......而且如果我按 F 的 J,它确实将它保存在一个变量中!它还可以节省反应时间! 无论如何,这是我的代码,为什么在第一次试用后不开始第二次试用 运行ning 很棒?
#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.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
这样的属性。解决方案只是将上一行中的变量重命名为不同于 "sound".
附带说明一下,您似乎在每次试验中都创建了相同的声音对象。 sound1
和你现在所说的sound
都是sound.SoundPygame(value=stimulussound)
。为什么不只拥有一个?