![](/img/trans.png)
[英]python pygame mixer cant open wav file error on mac and pygame is not defined
[英]Pygame error: not able to open .wav file
我的 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:
無法打開文件“correct.wav”
我遇到了同樣的問題,無法使此代碼正常工作:
correctSound = pygame.mixer.Sound('jingle3.wav')
我收到 pygame 錯誤:
無法打開文件“jingle3.wav”
通過將“jingle3.wav”加載到 Audicity 並將其從 32 位 PCM 更改為 16 位 PCM .wav 文件解決了該問題。 之后 pygame.mixer.Sound 完美運行。
我在這個論壇上得到了這個提示,它奏效了!
而不是在評論部分進行討論,我會發布這個。
一旦def wait_finish(channnel):
已更改為def wait_finish(channel):
並且sounds.Sound
而不是sounds.Sounds
問題已經解決,程序就可以在我的機器上正常使用普通的.wav 文件。
我確信 Sound 或 Sounds 調用了要播放的錯誤.wav 文件的錯誤會解釋“無法打開文件錯誤.wav”的消息。
如果 pygame 不喜歡該文件的某些內容,它很可能不會播放它,這是如下一行:
sounds.pre_init(frequency=22050, size=-16, channels=2, buffer=4096)
(在sounds.init()
之前sounds.init()
)可能會發揮作用。
(注意,您可能必須使用不同的緩沖區選項,因為我正在使用 pygame for python 2.7 進行測試)
在我的盒子上,如果 pygame 不喜歡或找不到該文件,我根本不會收到任何錯誤,但是在調用播放時揚聲器會發出咔噠聲。
在這一點上,我只能建議您嘗試使用與當前正在使用的完全不同的 wav 文件來獲取錯誤答案,看看它是否有所作為。 只是為了記錄,我將您的 wait_finish 函數更改為:
def wait_finish(channel):
while channel.get_busy():
pygame.time.Clock().tick(10)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.