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中正则表达式的用法实例汇总
Aug 18 Python
python实现从字符串中找出字符1的位置以及个数的方法
Aug 25 Python
采用Psyco实现python执行速度提高到与编译语言一样的水平
Oct 11 Python
linux 下实现python多版本安装实践
Nov 18 Python
在Python中定义和使用抽象类的方法
Jun 30 Python
基于python爬虫数据处理(详解)
Jun 10 Python
Python获取SQLite查询结果表列名的方法
Jun 21 Python
Python读csv文件去掉一列后再写入新的文件实例
Dec 28 Python
python模块smtplib实现纯文本邮件发送功能
May 22 Python
Python numpy.zero() 初始化矩阵实例
Nov 27 Python
python使用布隆过滤器的实现示例
Aug 20 Python
python Timer 类使用介绍
Dec 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
修改WordPress中文章编辑器的样式的方法详解
2015/12/15 PHP
php批量删除操作(数据访问)
2017/05/23 PHP
PHP基于DOMDocument解析和生成xml的方法分析
2017/07/17 PHP
phpQuery采集网页实现代码实例
2020/04/02 PHP
Extjs Ajax 乱码问题解决方案
2009/04/15 Javascript
JavaScript 对象的属性和方法4种不同的类型
2010/03/19 Javascript
JavaScript聚焦于第一个字段的代码
2010/10/15 Javascript
来自国外的30个基于jquery的Web下拉菜单
2012/06/22 Javascript
jquery实现简单的自动播放幻灯片效果
2015/06/13 Javascript
javascript实现移动端上的触屏拖拽功能
2016/03/04 Javascript
jquery中的常见问题及快速解决方法小结
2016/06/14 Javascript
Windows下快速搭建NodeJS本地服务器的步骤
2017/08/09 NodeJs
微信小程序实现选项卡功能
2020/06/19 Javascript
vue.js element-ui validate中代码不执行问题解决方法
2017/12/18 Javascript
使用Angular CLI进行Build(构建)和Serve详解
2018/03/24 Javascript
js实现点击展开隐藏效果(实例代码)
2018/09/28 Javascript
react组件从搭建脚手架到在npm发布的步骤实现
2019/01/09 Javascript
小程序getLocation需要在app.json中声明permission字段
2019/04/04 Javascript
jQuery实现带进度条的轮播图
2020/09/13 jQuery
浅析vue中的nextTick
2020/12/28 Vue.js
React服务端渲染原理解析与实践
2021/03/04 Javascript
从零学Python之入门(三)序列
2014/05/25 Python
在Python中利用Pandas库处理大数据的简单介绍
2015/04/07 Python
python 连接sqlite及简单操作
2017/06/30 Python
python去除字符串中的换行符
2017/10/11 Python
将pip源更换到国内镜像的详细步骤
2019/04/07 Python
Python中实现输入超时及如何通过变量获取变量名
2020/01/18 Python
美国最大的万圣节服装网站:HalloweenCostumes.com
2017/10/12 全球购物
澳大利亚个性化儿童礼品网站:Bright Star Kids
2019/06/14 全球购物
类、抽象类、接口的差异
2016/06/13 面试题
毕业生实习鉴定
2013/12/11 职场文书
机关门卫岗位职责
2013/12/30 职场文书
中层竞聘演讲稿
2014/01/09 职场文书
银行贷款承诺书
2014/03/29 职场文书
政工例会汇报材料
2014/08/26 职场文书
大学生社会实践感想
2015/08/11 职场文书