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捕捉和模拟鼠标事件的方法
Jun 03 Python
Python创建xml文件示例
Mar 22 Python
Python编写一个闹钟功能
Jul 11 Python
python 统计列表中不同元素的数量方法
Jun 29 Python
python退出命令是什么?详解python退出方法
Dec 10 Python
Python3中lambda表达式与函数式编程讲解
Jan 14 Python
Python使用mongodb保存爬取豆瓣电影的数据过程解析
Aug 14 Python
基于TensorFlow中自定义梯度的2种方式
Feb 04 Python
python 已知平行四边形三个点,求第四个点的案例
Apr 12 Python
Python爬取阿拉丁统计信息过程图解
May 12 Python
Python3.7安装PyQt5 运行配置Pycharm的详细教程
Oct 15 Python
如何通过Python实现RabbitMQ延迟队列
Nov 28 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 session有效期问题
2009/04/26 PHP
php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29"))代码解密
2011/01/07 PHP
PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍
2011/09/11 PHP
php格式输出文件var_export函数实例
2014/11/15 PHP
PHP+Ajax+JS实现多图上传
2016/05/07 PHP
Yii实现Command任务处理的方法详解
2016/07/14 PHP
PHP+Ajax实现的博客文章添加类别功能示例
2018/03/29 PHP
jQuery 性能优化指南(3)
2009/05/21 Javascript
document.write()及其输出内容的样式、位置控制
2013/08/12 Javascript
js实现飞入星星特效代码
2014/10/17 Javascript
js实现屏幕自适应局部代码分享
2015/01/30 Javascript
jquery实现动态操作select选中
2015/02/11 Javascript
html+js实现简单的计算器代码(加减乘除)
2016/07/12 Javascript
JS实现提交表单前的数字及邮箱校检功能
2017/11/13 Javascript
关于Google发布的JavaScript代码规范你要知道哪些
2018/04/04 Javascript
layui框架与SSM前后台交互的方法
2019/09/12 Javascript
vue3.0中使用postcss-pxtorem的具体方法
2019/11/20 Javascript
在Vue 中获取下拉框的文本及选项值操作
2020/08/13 Javascript
安装Python的教程-Windows
2017/07/22 Python
ubuntu中配置pyqt4环境教程
2017/12/27 Python
python实现基于朴素贝叶斯的垃圾分类算法
2019/07/09 Python
Python操作多维数组输出和矩阵运算示例
2019/11/28 Python
Keras实现DenseNet结构操作
2020/07/06 Python
详解CSS3选择器的使用方法汇总
2015/11/24 HTML / CSS
检测浏览器是否支持html5视频的代码
2013/03/28 HTML / CSS
添柏岚英国官方网站:Timberland英国
2019/11/28 全球购物
.net C#面试题
2012/08/28 面试题
艺术设计专业个人求职信
2013/09/21 职场文书
教师个人自我鉴定
2014/02/08 职场文书
幼儿园师德演讲稿
2014/05/06 职场文书
优秀党支部书记事迹材料
2014/05/29 职场文书
住房租房协议书
2014/08/20 职场文书
销售代理协议书
2014/09/30 职场文书
你对自己的信用报告有过了解吗?
2019/07/09 职场文书
MySQL基础(二)
2021/04/05 MySQL
HTML中link标签属性的具体用法
2023/05/07 HTML / CSS