[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] playing MP3s using pygame



Prahlad Vaidyanathan wrote:
> Someone on the python-tutor list mentioned that I could play MP3s using
> pygame. So, I built pygame with SDL_mixer, and did the following :
>
>>>>file = '/home/prahlad/test/test.mp3'
>>>>import pygame.mixer
>>>>pygame.mixer.init()
>>>>sf = pygame.mixer.Sound(file)

pygame does support MP3 playback. you're SDL_mixer library will need to 
be configured to use the optional SMPEG library to do the actual MP3 
decoding.

SDL_mixer treats MP3 audio a little different than normal sounds. to 
play them you'll need to load the MP3 into pygame's streaming music 
channel. pygame offers a single "special" audio channel that streams 
music as it plays. (a good idea for 5 minute sound files). it is the 
"music" object in the mixer module.
to do this, your code needs a small change...

     import pygame.mixer
     pygame.mixer.init()
     pygame.mixer.music.load('test.mp3')
     pygame.mixer.music.play()


this should get you running. i'll warn you that you'll find the 
occasional MP3 that doesn't work properly with SMPEG (i think it's the 
variable-bitrate ones, but i'm not really sure).

when you make the mixer.init() call, you can also pass some arguments, 
like what frequency the mixer should run at. you'll probably want this 
for mp3 playback...

     pygame.mixer.init(44100)




____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org