python怎么播放音乐


本文摘自php中文网,作者尚,侵删。

Python播放音乐要使用pygame模块
首先安装pygame模块:pip install pygame
方法实现:

1

2

3

4

5

6

7

8

9

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

想要去掉此打印,在类的上方添加如下代码

1

2

from os import environ

environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是python怎么播放音乐的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python学习日记(50)--paramiko

没学过编程可以学Python

电脑从cmd中怎么进不去Python

如何使用Python画曲线图

利用Python如何爬取js里面的内容

Python day05 Python 环境变量和import模块导入

Python threading模块能做什么?(实例详解)

Python中怎么向字典添加元素

Python:中input()与raw_input()的详解

pycharm怎么用Python

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...