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中的多进程与多线程的使用
Apr 07 Python
Python中用sleep()方法操作时间的教程
May 22 Python
开始着手第一个Django项目
Jul 15 Python
python 类详解及简单实例
Mar 24 Python
python实现音乐下载的统计
Jun 20 Python
Django2.1集成xadmin管理后台所遇到的错误集锦(填坑)
Dec 20 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
Mar 27 Python
python 非线性规划方式(scipy.optimize.minimize)
Feb 11 Python
python 解决print数组/矩阵无法完整输出的问题
Feb 19 Python
Python类的动态绑定实现原理
Mar 21 Python
Python中json.load()和json.loads()有哪些区别
Jun 07 Python
Python实现猜拳与猜数字游戏的方法详解
Apr 06 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 结果集的分页实现代码
2009/03/10 PHP
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
2011/12/05 PHP
ThinkPHP模板判断输出Present标签用法详解
2014/06/30 PHP
PHP开发框架kohana3 自定义路由设置示例
2014/07/14 PHP
php自定义时间转换函数示例
2016/12/07 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
HTML中事件触发列表与解说
2007/07/09 Javascript
JS 动态加载脚本的4种方法
2009/05/05 Javascript
分享XmlHttpRequest调用Webservice的一点心得
2012/07/20 Javascript
javascript 弹出的窗口返回值给父窗口具体实现
2013/11/23 Javascript
js 弹出框只弹一次(二次修改之后的)
2013/11/26 Javascript
原生Js实现简易烟花爆炸效果的方法
2015/03/20 Javascript
微信小程序学习(4)-系统配置app.json详解
2017/01/12 Javascript
Angularjs中使用轮播图指令swiper
2017/05/30 Javascript
Vue2.0实现购物车功能
2017/06/05 Javascript
JS中的BOM应用
2018/02/02 Javascript
详解用Node.js写一个简单的命令行工具
2018/03/01 Javascript
在vue项目中,使用axios跨域处理
2018/03/07 Javascript
Vue使用高德地图搭建实时公交应用功能(地图 + 附近站点+线路详情 + 输入提示+换乘详情)
2018/05/16 Javascript
JavaScript如何借用构造函数继承
2019/11/06 Javascript
vue 实现强制类型转换 数字类型转为字符串
2019/11/07 Javascript
jQuery实现二级导航菜单的示例
2020/09/30 jQuery
微信小程序实现简单的select下拉框
2020/11/23 Javascript
[02:44]2014DOTA2 国际邀请赛中国区预选赛 大神红毯秀
2014/05/25 DOTA
[02:21]十步杀一人,千里不留行——DOTA2全新英雄天涯墨客展示
2018/08/29 DOTA
python编程开发之类型转换convert实例分析
2015/11/13 Python
简单易懂的python环境安装教程
2017/07/13 Python
浅谈解除装饰器作用(python3新增)
2018/10/15 Python
如何用Matlab和Python读取Netcdf文件
2021/02/19 Python
南威尔士家居商店:Leekes
2016/10/25 全球购物
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
2015年“世界无车日”活动方案
2015/05/06 职场文书
2015年车间安全管理工作总结
2015/05/13 职场文书
2016年心理学教育培训学习心得体会
2016/01/12 职场文书
python字符串常规操作大全
2021/05/02 Python
SQL Server中的逻辑函数介绍
2022/05/25 SQL Server