[英]python pygame mixer cant open wav file error on mac and pygame is not defined
我正在使用混音器打開文件,但出現錯誤。 我正在使用Mac OS X,這是我的代碼:
mixer.init()
mixer.pre_init(44100, 16, 2, 4096)
mixer.music.load('Warning.wav')
mixer.music.play()
我得到錯誤:
無法打開“ Warning.wav”。
而且,當我編寫pygame.mixer.init()
,我也得到了錯誤:
未定義名稱“ pygame”
我怎么解決這個問題? 還可以為我更改代碼嗎? 我需要盡快解決方案!
首先導入並初始化pygame。 這兩行代碼必須存在。
import pygame pygame.init()
現在,初始化混音器。
pygame.mixer.init()
加載音樂。
pygame.mixer.load("musicfile.wav")
播放音樂。
pygame.mixer.play(-1)
參數-1將告訴它永遠播放。
您還需要將屏幕設置為變量。
gameDisplay = pygame.display.set_mode((640, 480))
這是一個完整的示例。
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((640, 480))
pygame.mixer.init()
pygame.mixer.music.load("resources\\music.mp3")
pygame.mixer.music.play()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
希望這對您有所幫助!
注意:Pygame與.mp3文件(用於音樂)和.wav文件(用於聲音效果)一起使用時效果更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.