How can I play an mp3 with pygame?

2019-01-09 01:32发布

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?

标签: python pygame
7条回答
相关推荐>>
2楼-- · 2019-01-09 01:59

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)
查看更多
登录 后发表回答