How can I play an mp3 with pygame?

Solution 1:

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)

Solution 2:

The music stops because it's an asyncronous event, which means it'll keep going with the script. then, the script stops instantly, not giving the music a chance to start. as stated before, you could use

while pygame.mixer.music.get_busy(): 
  pygame.time.Clock().tick(10)

however, even better is pygame.event.wait(), as it'll wait for all asynchronous events to end.

Solution 3:

Here is a super easy way.

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