一、运行环境
python 3.7.4
pygame 2.1.2
pycharm 社区版
二、代码
def music(self):
music_array=["精灵村.mp3","猎人村.mp3","说话岛.mp3","芙罗兰村庄.mp3"
,"水上都市海因斯.mp3","奇岩城镇.mp3","古鲁丁村庄.mp3","亚丁城镇.mp3"
,"命运的召唤.mp3","CrossroadAtDawn.mp3","AfterTheStorm.mp3"]
index=random.randint(0, len(music_array)-1)
#file = Tools.get_resource_mp3_path() os.sep music_array[index]
file = "D:\Code\Python\AdenHunter\framework\resource\mp3" os.sep music_array[index]
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play(loops=10) # pygame.mixer.music.play(-1)
while pygame.mixer.music.get_busy(): # 在音频播放为完成之前不退出程序
pass
这样会线程阻塞多线程的我也写了
T = threading.Thread(target=self.music, args=())
T.setDaemon(True)
T.start()
哈哈毫无难度,MP3用自己本地的就好了