无法加载音乐

Unable to load music

我尝试了使用 pygame 播放音乐的非常简单的代码,但是 cmd 启动然后立即关闭,没有提供任何实质性输出。请在下面找到代码。

from pygame import mixer

mixer.init()
mixer.music.load("elvis.mp3")
mixer.music.play()

pygame docs 中说:

Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.

因此请尝试将文件转换为另一种格式,例如 WAV 或 OGG。

您也可以使用文件的绝对路径: full/path/to/your/file/elvis.mp3

还要确保 elvis.mp3 在您的项目文件夹中。

您必须等待音乐播放完毕:

from pygame import mixer
import time

mixer.init()
mixer.music.load("elvis.mp3")
mixer.music.play()

while mixer.music.get_busy():
   time.sleep(0.01)

mixer.quit()