Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play an mp3 with pygame?

Tags:

python

pygame

import pygame file = 'some.mp3' pygame.init() pygame.mixer.init() pygame.mixer.music.load(file) pygame.mixer.music.play() 

This outputs, "Process finished with exit code 0", but it doesn't play anything. How can I resolve this problem?

like image 432
Ashot Avatar asked Oct 12 '11 20:10

Ashot


People also ask

Can pygame play mp3 files?

Pygame can load WAV, MP3, or OGG files. The difference between these audio file formats is explained at http://invpy.com/formats. To play this sound, call the Sound object's play() method. If you want to immediately stop the Sound object from playing call the stop() method.

How do I play music on pygame?

Invoke pygame. mixer. music. play() to start playback of the music stream.


1 Answers

The play function starts the music playing, but returns immediately. Then your program reaches it's end, and the pygame object is automatically destroyed which causes the music to stop.

As you commented, it does play the music if you wait for it before exiting - because then the pygame object isn't destroyed until the while loop finishes.

while pygame.mixer.music.get_busy():      pygame.time.Clock().tick(10) 
like image 58
Ichigo Jam Avatar answered Oct 07 '22 12:10

Ichigo Jam