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 相关文章推荐
easy_install python包安装管理工具介绍
Feb 10 Python
浅谈Python处理PDF的方法
Nov 10 Python
django模板加载静态文件的方法步骤
Mar 01 Python
Django实现跨域的2种方法
Jul 31 Python
Python数据可视化:幂律分布实例详解
Dec 07 Python
Python 调用有道翻译接口实现翻译
Mar 02 Python
pycharm新建Vue项目的方法步骤(图文)
Mar 04 Python
keras-siamese用自己的数据集实现详解
Jun 10 Python
如何理解python对象
Jun 21 Python
Python爬虫回测股票的实例讲解
Jan 22 Python
OpenCV-Python实现怀旧滤镜与连环画滤镜
Jun 09 Python
总结几个非常实用的Python库
Jun 26 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模板中出现空行解决方法
2011/03/08 PHP
PHP命名空间namespace用法实例分析
2016/09/27 PHP
PHP 闭包详解及实例代码
2016/09/28 PHP
js跨域和ajax 跨域问题的实现思路
2009/09/05 Javascript
JavaScript 动态添加表格行 使用模板、标记
2009/10/24 Javascript
jQuery中:last-child选择器用法实例
2014/12/31 Javascript
jquery操作复选框checkbox的方法汇总
2015/02/05 Javascript
图文详解Heap Sort堆排序算法及JavaScript的代码实现
2016/05/04 Javascript
js倒计时简单实现代码
2016/08/11 Javascript
用js实现博客打赏功能
2016/10/24 Javascript
AngularJS基于provider实现全局变量的读取和赋值方法
2017/06/28 Javascript
简述vue-cli中chainWebpack的使用方法
2019/07/30 Javascript
javascript(基于jQuery)实现鼠标获取选中的文字示例【测试可用】
2019/10/26 jQuery
vue 实现购物车总价计算
2019/11/06 Javascript
vue-router的hooks用法详解
2020/06/08 Javascript
js编写简易的计算器
2020/07/29 Javascript
vue+elementUI(el-upload)图片压缩,默认同比例压缩操作
2020/08/10 Javascript
[13:21]DOTA2国际邀请赛采访专栏:RSnake战队国士无双,Fnatic.Fly
2013/08/06 DOTA
[01:01:51]EG vs VG Supermajor小组赛B组 BO3 第二场 6.2
2018/06/03 DOTA
Python编写电话薄实现增删改查功能
2016/05/07 Python
python logging 日志轮转文件不删除问题的解决方法
2016/08/02 Python
pyQt5实时刷新界面的示例
2019/06/25 Python
Apache,wsgi,django 程序部署配置方法详解
2019/07/01 Python
Python 给定的经纬度标注在地图上的实现方法
2019/07/05 Python
IE浏览器单独写CSS样式的几种方法
2014/10/14 HTML / CSS
详解CSS3中nth-child与nth-of-type的区别
2017/01/05 HTML / CSS
中国双语服务优势的在线购票及活动平台:247tickets
2018/10/26 全球购物
如何用Python来进行查询和替换一个文本字符串
2014/01/02 面试题
信用社实习人员自我鉴定
2013/09/20 职场文书
医药类个人求职的自我评价
2014/02/12 职场文书
优秀乡村医生事迹材料
2014/05/28 职场文书
给校长的一封检讨书
2014/09/20 职场文书
考试作弊万能检讨书
2014/10/19 职场文书
我们的节日中秋节活动总结
2015/03/23 职场文书
信访维稳承诺书
2015/05/04 职场文书
使用@Value值注入及配置文件组件扫描
2021/07/09 Java/Android