How can I play an mp3 with pygame?

Ashot picture Ashot · Oct 12, 2011 · Viewed 66.7k times · Source
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?

Answer

Ichigo Jam picture Ichigo Jam · Dec 7, 2011

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)