python实现播放音乐

使用pygame模块
首先安装 pip install pygame
方法实现
**

def play_music(self):
        filepath = r"E:\music\消愁.mp3";
        pygame.mixer.init()
        # 加载音乐
        pygame.mixer.music.load(filepath)
        pygame.mixer.music.play(start=0.0)
        #播放时长,没有此设置,音乐不会播放,会一次性加载完
        time.sleep(300)
        pygame.mixer.music.stop()

**
注意:
1.必须要有time.sleep(time),否则音乐不会播放,亲测
2.首次使用,会遇到一个问题,就是控制台会打印下面内容
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
想要去掉此打印,在类的上方添加如下代码

from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

你可能感兴趣的:(python,播放音乐,Python)