[英]Pygame error: not able to open .wav file
I have a problem with my Pygame program.我的 Pygame 程序有问题。 I need help.我需要帮助。 The wav file is in the same directory as the python file. wav 文件与 python 文件在同一目录中。 I run It in terminal- Python3:我在终端 Python3 中运行它:
import pygame.mixer
sounds = pygame.mixer
sounds.init()
def wait_finish(channel):
while channel.get_busy():
pass
asked = 0
true = 0
false = 0
choice = str(input("Push 1 for true, 2 for false, 0 to end"))
while choice != '0':
if choice == '1':
asked = asked + 1
true = true + 1
s = sounds.Sound("correct.wav")
wait_finish(s.play())
if choice == '2':
asked = asked + 1
false = false + 1
s = sounds.Sound("wrong.wav")
wait_finish(s.play())
choice = str(input("Push 1 for true, 2 for false, 0 to end"))
print ("you asked" +str(asked) + "questions")
print ("there were" +str(false) + "wrong answers")
print ("and" + str(true) + "correct answers")
It throws- pygame.error:它抛出 - pygame.error:
Unable to open file 'correct.wav'无法打开文件“correct.wav”
I had the same problem, could not get this code to work:我遇到了同样的问题,无法使此代码正常工作:
correctSound = pygame.mixer.Sound('jingle3.wav')
I got pygame error:我收到 pygame 错误:
Unable to open file 'jingle3.wav'无法打开文件“jingle3.wav”
The problem was solved by loading the 'jingle3.wav' into Audicity and changing it from 32-bit PCM to a 16-bit PCM .wav-file.通过将“jingle3.wav”加载到 Audicity 并将其从 32 位 PCM 更改为 16 位 PCM .wav 文件解决了该问题。 After that the pygame.mixer.Sound worked perfectly.之后 pygame.mixer.Sound 完美运行。
I got this tip on this forum, and it worked!我在这个论坛上得到了这个提示,它奏效了!
Rather than have a discussion in the comments section I'll post this.而不是在评论部分进行讨论,我会发布这个。
Once def wait_finish(channnel):
has been altered to def wait_finish(channel):
and the issue with sounds.Sound
rather than sounds.Sounds
has been resolved, the program works fine with normal .wav files on my machine.一旦def wait_finish(channnel):
已更改为def wait_finish(channel):
并且sounds.Sound
而不是sounds.Sounds
问题已经解决,程序就可以在我的机器上正常使用普通的.wav 文件。
I am convinced that the error of Sound or Sounds for calling the wrong.wav file to be played would explain the "unable to open file wrong.wav" message.我确信 Sound 或 Sounds 调用了要播放的错误.wav 文件的错误会解释“无法打开文件错误.wav”的消息。
If pygame doesn't like something about the file it may well not play it and this is where a line like:如果 pygame 不喜欢该文件的某些内容,它很可能不会播放它,这是如下一行:
sounds.pre_init(frequency=22050, size=-16, channels=2, buffer=4096)
(called before sounds.init()
) might come into play. (在sounds.init()
之前sounds.init()
)可能会发挥作用。
(NB you might have to use a different buffer option as I am testing with pygame for python 2.7) (注意,您可能必须使用不同的缓冲区选项,因为我正在使用 pygame for python 2.7 进行测试)
On my box, if pygame doesn't like or cannot find the file, I get no error at all but the speakers click when the call to play is made.在我的盒子上,如果 pygame 不喜欢或找不到该文件,我根本不会收到任何错误,但是在调用播放时扬声器会发出咔哒声。
All I can suggest at this point is that you try an entirely different wav file to the current one you are using for wrong answers and see if it makes a difference.在这一点上,我只能建议您尝试使用与当前正在使用的完全不同的 wav 文件来获取错误答案,看看它是否有所作为。 Just for the record, I changed your wait_finish function to :只是为了记录,我将您的 wait_finish 函数更改为:
def wait_finish(channel):
while channel.get_busy():
pygame.time.Clock().tick(10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.