python使用PyGame播放Midi和Mp3文件的方法


Posted in Python onApril 24, 2015

本文实例讲述了python使用PyGame播放Midi和Mp3文件的方法。分享给大家供大家参考。具体实现方法如下:

''' pg_midi_sound101.py
play midi music files (also mp3 files) using pygame
tested with Python273/331 and pygame192 by vegaseat
'''
import pygame as pg
def play_music(music_file):
  '''
  stream music with mixer.music module in blocking manner
  this will stream the sound from disk while playing
  '''
  clock = pg.time.Clock()
  try:
    pg.mixer.music.load(music_file)
    print("Music file {} loaded!".format(music_file))
  except pygame.error:
    print("File {} not found! {}".format(music_file, pg.get_error()))
    return
  pg.mixer.music.play()
  # check if playback has finished
  while pg.mixer.music.get_busy():
    clock.tick(30)
# pick a midi or MP3 music file you have in the working folder
# or give full pathname
music_file = "Latin.mid"
#music_file = "Drumtrack.mp3"
freq = 44100  # audio CD quality
bitsize = -16  # unsigned 16 bit
channels = 2  # 1 is mono, 2 is stereo
buffer = 2048  # number of samples (experiment to get right sound)
pg.mixer.init(freq, bitsize, channels, buffer)
# optional volume 0 to 1.0
pg.mixer.music.set_volume(0.8)
try:
  play_music(music_file)
except KeyboardInterrupt:
  # if user hits Ctrl/C then exit
  # (works only in console mode)
  pg.mixer.music.fadeout(1000)
  pg.mixer.music.stop()
  raise SystemExit

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python实现爬取逐浪小说的方法
Jul 07 Python
浅谈python中列表、字符串、字典的常用操作
Sep 19 Python
python取代netcat过程分析
Feb 10 Python
python中找出numpy array数组的最值及其索引方法
Apr 17 Python
python实现多进程代码示例
Oct 31 Python
Python爬虫实现爬取百度百科词条功能实例
Apr 05 Python
Django学习笔记之为Model添加Action
Apr 30 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
python实现最速下降法
Mar 24 Python
Python Django路径配置实现过程解析
Nov 05 Python
Python天气语音播报小助手
Sep 25 Python
python对文档中元素删除,替换操作
Apr 02 Python
python使用PyGame绘制图像并保存为图片文件的方法
Apr 24 #Python
python使用PIL缩放网络图片并保存的方法
Apr 24 #Python
python使用Tkinter显示网络图片的方法
Apr 24 #Python
Python中最常用的操作列表的几种方法归纳
Apr 24 #Python
在Python中使用lambda高效操作列表的教程
Apr 24 #Python
使用Python的判断语句模拟三目运算
Apr 24 #Python
Python的字典和列表的使用中一些需要注意的地方
Apr 24 #Python
You might like
PHP批量上传图片的具体实现方法介绍.
2014/02/26 PHP
php和editplus正则表达式去除空白行
2015/04/17 PHP
PHP随机生成唯一HASH值自定义函数
2015/04/20 PHP
PHP获取指定月份第一天和最后一天的方法
2015/07/18 PHP
php中array_slice和array_splice函数解析
2016/10/18 PHP
PHP表单验证内容是否为空的实现代码
2016/11/14 PHP
枚举的实现求得1-1000所有出现1的数字并计算出现1的个数
2013/09/10 Javascript
用js实现in_array的方法
2013/11/05 Javascript
JS实现遮罩层效果的简单实例
2013/11/12 Javascript
写出高效jquery代码的19条指南
2014/03/19 Javascript
JQuery实现防止退格键返回的方法
2015/02/12 Javascript
jQuery实现的文字hover颜色渐变效果实例
2016/02/20 Javascript
JavaScript利用闭包实现模块化
2017/01/13 Javascript
使用vs code开发Nodejs程序的使用方法
2017/09/21 NodeJs
深入理解requireJS-实现一个简单的模块加载器
2018/01/15 Javascript
微信小程序实现人脸检测功能
2018/05/25 Javascript
apicloud拉起小程序并传递参数的方法示例
2018/11/21 Javascript
vue视频播放插件vue-video-player的具体使用方法
2019/11/08 Javascript
nodejs使用Sequelize框架操作数据库的实现
2020/10/21 NodeJs
python中的对象拷贝示例 python引用传递
2014/01/23 Python
深入理解Python 代码优化详解
2014/10/27 Python
对Python3之方法的覆盖与super函数详解
2019/06/26 Python
python Qt5实现窗体跟踪鼠标移动
2019/12/13 Python
如何表示python中的相对路径
2020/07/08 Python
韩国女装NO.1网店:STYLENANDA
2016/09/16 全球购物
世界上最大的字体市场:MyFonts
2020/01/10 全球购物
学生自我评价范文
2014/02/02 职场文书
小溪流的歌教学反思
2014/02/13 职场文书
幼儿园新年寄语
2014/04/03 职场文书
大学生心理活动总结
2014/07/04 职场文书
2014年打非治违工作总结
2014/11/13 职场文书
整改通知书
2015/04/20 职场文书
工伤认定行政答辩状
2015/05/22 职场文书
生活小常识广播稿
2015/08/19 职场文书
熟背这些句子,让您的英语口语突飞猛进(135句)
2019/09/06 职场文书
你真的了解PHP中的引用符号(&)吗
2021/05/12 PHP