我正在尝试Making Games with Python & Pygame书中的一些基本例子,但我面临一个奇怪的问题。以下是示例来源:
import pygame, time
soundObj = pygame.mixer.Sound('beep.wav')
soundObj.play()
time.sleep(1) # wait and let the sound play for 1 second
soundObj.stop()
此来源产生以下错误:
Traceback(最近一次调用最后一次):文件 “C:/Users/Thiago/PycharmProjects/PyGame/Sound/app.py”,第3行,在 soundObj = pygame.mixer.Sound('beep.wav')pygame.error:无法打开文件'beep.wav'
beep.wav 文件已正确保存在我的Python脚本的同一文件夹中。我已经尝试了os.listdir()
命令,它返回了wav文件。是否存在任何问题,已知错误或我做错了什么?
这是我的环境:
答案 0 :(得分:1)
您必须先初始化模块或所有pygame。 有一个pygame_init()初始化程序可以帮助你。 你可以找到它here
答案 1 :(得分:0)
我解决了这个问题,添加pygame.init()
初始化Pygame和pygame.display.set_mode()
来创建窗口。
import pygame, time
pygame.init() # initialize the pygame
soundObj = pygame.mixer.Sound('beep.wav')
DISPLAYSURF = pygame.display.set_mode((400, 300)) # create the game window
soundObj.play()
time.sleep(1) # wait and let the sound play for 1 second
soundObj.stop()
答案 2 :(得分:0)
brew install libogg brew install libvorbis brew install sdl_mixer --with-libvorbis brew reinstall sdl_mixer --with-libvorbis
这个功能对我来说:D