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使用pil库实现图片合成实例代码
Jan 20 Python
Python2和Python3之间的str处理方式导致乱码的讲解
Jan 03 Python
python学习开发mock接口
Apr 28 Python
用Python从0开始实现一个中文拼音输入法的思路详解
Jul 20 Python
Django 反向生成url实例详解
Jul 30 Python
Pandas0.25来了千万别错过这10大好用的新功能
Aug 07 Python
Python如何实现强制数据类型转换
Nov 22 Python
python装饰器代替set get方法实例
Dec 19 Python
简单了解python调用其他脚本方法实例
Mar 26 Python
详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案
Jan 29 Python
Python 求向量的余弦值操作
Mar 04 Python
Python pyecharts案例超市4年数据可视化分析
Aug 14 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应用JSON技巧讲解
2013/02/03 PHP
php strnatcmp()函数的用法总结
2013/11/27 PHP
php数组中包含中文的排序方法
2014/06/03 PHP
JS实现根据当前文字选择返回被选中的文字
2014/05/21 Javascript
前端必备神器 Snap.svg 弹动效果
2014/11/10 Javascript
jQuery元素的隐藏与显示实例
2015/01/20 Javascript
jquery实现的省市区三级联动
2015/04/02 Javascript
JavaScript使用二分查找算法在数组中查找数据的方法
2015/04/07 Javascript
论JavaScript模块化编程
2016/03/07 Javascript
JavaScript判断数组是否存在key的简单实例
2016/08/03 Javascript
实例解析Array和String方法
2016/12/14 Javascript
源码分析Vue.js的监听实现教程
2017/04/23 Javascript
jquery实现提示语淡入效果
2017/05/05 jQuery
vue自定义全局组件(自定义插件)的用法
2018/01/30 Javascript
谈谈我在vue-cli3中用预渲染遇到的坑
2020/04/22 Javascript
Vue自动构建发布脚本的方法示例
2020/07/24 Javascript
Element Popover 弹出框的使用示例
2020/07/26 Javascript
[07:25]DOTA2-DPC中国联赛2月5日Recap集锦
2021/03/11 DOTA
python比较2个xml内容的方法
2015/05/11 Python
Python判断变量是否为Json格式的字符串示例
2017/05/03 Python
Python实现求两个csv文件交集的方法
2017/09/06 Python
python学习必备知识汇总
2017/09/08 Python
使用NumPy和pandas对CSV文件进行写操作的实例
2018/06/14 Python
python经典趣味24点游戏程序设计
2019/07/26 Python
vscode写python时的代码错误提醒和自动格式化的方法
2020/05/07 Python
Tensorflow--取tensorf指定列的操作方式
2020/06/30 Python
关于PyCharm安装后修改路径名称使其可重新打开的问题
2020/10/20 Python
利用Python如何画一颗心、小人发射爱心
2021/02/21 Python
详解HTML5将footer置于页面最底部的方法(CSS+JS)
2018/10/11 HTML / CSS
煤矿班组长的职责
2013/12/25 职场文书
寒假思想汇报
2014/01/10 职场文书
《风筝》教学反思
2014/04/10 职场文书
关于拾金不昧的感谢信
2015/01/21 职场文书
体育教师个人工作总结
2015/02/09 职场文书
Vue项目打包、合并及压缩优化网页响应速度
2021/07/07 Vue.js
AngularJS实现多级下拉框
2022/03/25 Javascript