Reputation: 255
I've been having problems trying to load and play sounds in pygame. Most people seem to have trouble with MP3s but for some reason, I can't even get wavs to play.
>>> f=open('menuscreen.wav',"rb")
>>> pygame.mixer.init()
>>> pygame.mixer.music.load(f)
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
pygame.error: Module format not recognized
and neither does this work:
>>> k = pygame.mixer.Sound('menuscreen.wav')
>>> pygame.mixer.init()
>>> pygame.mixer.music.load(k)
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
pygame.error: Couldn't read from RWops
and neither does this:
>>> import pygame
>>> pygame.mixer.init()
>>> pygame.mixer.music.load('menuscreen.wav')
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
pygame.error: Unable to load WAV file
I'm using 2.7.2 and pygame 1.9.1
Upvotes: 4
Views: 9087
Reputation: 11
To avoid writing every line of code pygame.mixer
you can also do:
from pygame import mixer
mixer.init()
mixer.music.load('songX.mp3')
mixer.music.play()
(as a shorter version of @Mac's answer)
Upvotes: 0
Reputation: 43061
You should load the file by passing the file name as first argument. This works on my system (same python and pygame versions) just fine:
>>> import pygame
>>> pygame.mixer.init()
>>> pygame.mixer.music.load('filename.wav')
>>> pygame.mixer.music.play()
HTH!
Upvotes: 6