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 02 Python
python 自动化将markdown文件转成html文件的方法
Sep 23 Python
Python中elasticsearch插入和更新数据的实现方法
Apr 01 Python
Python实现登陆文件验证方法
Oct 06 Python
python中退出多层循环的方法
Nov 27 Python
Django unittest 设置跳过某些case的方法
Dec 26 Python
windows10下安装TensorFlow Object Detection API的步骤
Jun 13 Python
python实现在函数图像上添加文字和标注的方法
Jul 08 Python
tornado+celery的简单使用详解
Dec 21 Python
详解Pycharm出现out of memory的终极解决方法
Mar 03 Python
Python面向对象程序设计之继承、多态原理与用法详解
Mar 23 Python
Pycharm自动添加文件头注释和函数注释参数的方法
Oct 23 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 similar_text 字符串的相似性比较函数
2010/05/26 PHP
php全角字符转换为半角函数
2014/02/07 PHP
PHP开发微信支付的代码分享
2014/05/25 PHP
PHP生成网站桌面快捷方式代码分享
2014/10/11 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2014/11/06 PHP
PHP实现加密文本文件并限制特定页面的存取的效果
2016/10/21 PHP
PHP环形链表实现方法示例
2017/09/15 PHP
Ruffy javascript 学习笔记
2009/11/30 Javascript
range 标准化之获取
2011/08/28 Javascript
jQuery实现的Div窗口震动特效
2014/06/09 Javascript
JavaScript实现列表分页功能特效
2015/05/15 Javascript
jQuery替换节点用法示例(使用replaceWith方法)
2016/09/08 Javascript
jquery删除table当前行的实例代码
2016/10/07 Javascript
jquery-mobile表单的创建方法详解
2016/11/23 Javascript
JS中绑定事件顺序(事件冒泡与事件捕获区别)
2017/01/24 Javascript
webpack入门+react环境配置
2017/02/08 Javascript
Koa2 之文件上传下载的示例代码
2018/03/29 Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
2020/06/23 Javascript
[01:13:51]TNC vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python 捕获shell脚本的输出结果实例
2017/01/04 Python
python获取当前用户的主目录路径方法(推荐)
2017/01/12 Python
Python对excel文档的操作方法详解
2018/12/10 Python
PyQT5 QTableView显示绑定数据的实例详解
2019/06/25 Python
python-视频分帧&多帧合成视频实例
2019/12/10 Python
Python实现验证码识别
2020/06/15 Python
Python计算矩阵的和积的实例详解
2020/09/10 Python
css3打造一款漂亮的卡哇伊按钮
2013/03/20 HTML / CSS
EGO Shoes美国/加拿大:英国时髦鞋类品牌
2018/08/04 全球购物
Can a struct inherit from another struct? (结构体能继承结构体吗)
2016/09/25 面试题
仓库管理制度
2014/01/21 职场文书
《油菜花开了》教学反思
2014/02/22 职场文书
环保建议书400字
2014/05/14 职场文书
技术入股合作协议书
2014/10/07 职场文书
写给医院的感谢信
2015/01/22 职场文书
2015年公司后勤管理工作总结
2015/05/13 职场文书
毕业生自荐求职信书写的技巧
2019/08/26 职场文书