简体   繁体   English

python pygame搅拌机无法在mac上打开wav文件错误,并且未定义pygame

[英]python pygame mixer cant open wav file error on mac and pygame is not defined

I'm using mixer to open file but I get a error. 我正在使用混音器打开文件,但出现错误。 I'm using mac os x.this is my code: 我正在使用Mac OS X,这是我的代码:

mixer.init()
mixer.pre_init(44100, 16, 2, 4096)
mixer.music.load('Warning.wav')
mixer.music.play()

and I get error: 我得到错误:

Couldn't open 'Warning.wav'. 无法打开“ Warning.wav”。

And also when I write pygame.mixer.init() , I get error too: 而且,当我编写pygame.mixer.init() ,我也得到了错误:

name 'pygame' is not defined 未定义名称“ pygame”

How can I solve this problem? 我怎么解决这个问题? And also can you change the code for me? 还可以为我更改代码吗? I need solution as fast as you can! 我需要尽快解决方案!

First of all import and initialize pygame. 首先导入并初始化pygame。 These two lines of code MUST be there. 这两行代码必须存在。

import pygame pygame.init()

Now, initialize the mixer. 现在,初始化混音器。

pygame.mixer.init()

Load the music. 加载音乐。

pygame.mixer.load("musicfile.wav")

Play the music. 播放音乐。

pygame.mixer.play(-1)

The parameter -1 will tell it to play forever. 参数-1将告诉它永远播放。

You also need to set a screen to a variable. 您还需要将屏幕设置为变量。

gameDisplay = pygame.display.set_mode((640, 480))

Here's a fully working example. 这是一个完整的示例。

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()

Hope this helped! 希望这对您有所帮助!

NOTE: Pygame works better with .mp3 files for music and .wav files for sound effects. 注意:Pygame与.mp3文件(用于音乐)和.wav文件(用于声音效果)一起使用时效果更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM