Python Pygame无法打开声音文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14845896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pygame cannot open sound file
提问by user2066880
import pygame, time
from pygame.locals import *
soundObj = pygame.mixer.Sound('beeps.wav')
soundObj.play()
time.sleep(1) # wait and let the sound play for 1 second
soundObj.stop()
and it throws this error:
它抛出这个错误:
Traceback (most recent call last):
File "C:/Users/Jauhar/Desktop/Python/sounds.py", line 4, in <module>
soundObj = pygame.mixer.Sound('beeps.wav')
pygame.error: Unable to open file 'beeps.wav'
The beeps.wav file is saved in the same directory as the python file the code is in.
beeps.wav 文件与代码所在的 python 文件保存在同一目录中。
I can't understand why it won't work!
我不明白为什么它不起作用!
采纳答案by Kaliber64
You cannot use pygames' library's unless you initialize either the modules your using or all of pygame.
您不能使用 pygames 的库,除非您初始化您使用的模块或所有 pygame。
pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize
pygame.init() #turn all of pygame on.
do these before you do anything in pygame. I recommend it.
在你在 pygame 中做任何事情之前先做这些。我推荐它。
回答by Aesthete
Try logging this information, and you may find your problem.
尝试记录此信息,您可能会发现问题。
import os
os.getcwd() # Log this line.
soundObj = pygame.mixer.Sound('beeps.wav')
This should tell you what directory your application is looking in when trying to access your sound file. You'll probably find it's at the base of your game directory.
这应该会告诉您您的应用程序在尝试访问您的声音文件时正在查找的目录。您可能会在游戏目录的底部找到它。
回答by phogren
Having a similar problem, I found that the size of the .wav file had a bearing on the problem. Making the .wav file smaller enabled it to work without making major modifications to the problem
遇到类似的问题,我发现 .wav 文件的大小与问题有关。使 .wav 文件更小使其无需对问题进行重大修改即可工作
回答by álvaro Serrano Arias
The same program works for me if I create a display surface before playing the sound.
如果我在播放声音之前创建一个显示表面,同样的程序对我有用。
#! Python 3
'''
Making games with python chapter 2 program 5
'''
import pygame, sys, time
from pygame.locals import*
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Sound!!")
soundObj = pygame.mixer.Sound('badswap.wav')
soundObj.play()
time.sleep(1) #wait and let the sound play for X second
soundObj.stop()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
回答by Getkey
Pygame (version 2.9 at least) doesn't support 32-bit float WAVs. Re-encode it to a signed 16-bit WAV (using Audacity for example).
Pygame(至少 2.9 版)不支持 32 位浮点 WAV。将其重新编码为有符号的 16 位 WAV(例如使用 Audacity)。
回答by Rose
Have the sound file after pygame.init(). I had this problem but after placing the sound file after that it worked just fine for me.
在 pygame.init() 之后有声音文件。我遇到了这个问题,但是在放置声音文件之后它对我来说效果很好。
回答by user5610278
This worked for me:
这对我有用:
pygame.mixer.init()
回答by kungp
Just encountered the same problem, and removing the ID tag from the wav file worked. So could be worth checking if the file has one!
刚遇到同样的问题,从 wav 文件中删除 ID 标签有效。所以值得检查文件是否有!

