Skip to main content
 首页 » 编程设计

python之Pygame错误: not able to open . wav文件

2025年05月04日41mate10pro

我的 Pygame 程序有问题。我需要帮助。 wav 文件与 python 文件位于同一目录中。 我在终端-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") 

它抛出- pygame.error:

Unable to open file 'correct.wav'

请您参考如下方法:

我有同样的问题,无法让这段代码工作:

correctSound = pygame.mixer.Sound('jingle3.wav') 

我遇到了 pygame 错误:

Unable to open file 'jingle3.wav'

通过将“jingle3.wav”加载到 Audicity 并将其从 32 位 PCM 更改为 16 位 PCM .wav 文件,问题得以解决。之后 pygame.mixer.Sound 完美运行。

我在这个论坛上得到了这个提示,它奏效了!