1

I'm using mixer to open file but I get a error. I'm using mac os x.this is my code:

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'.

And also when I write pygame.mixer.init(), I get error too:

name 'pygame' is not defined

How can I solve this problem? And also can you change the code for me? I need solution as fast as you can!

phd
  • 82,685
  • 13
  • 120
  • 165
민성우
  • 19
  • 2

1 Answers1

1

First of all import and initialize 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.

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.

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26