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 03 Python
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
Oct 18 Python
Python 数据结构之队列的实现
Jan 22 Python
Python 稀疏矩阵-sparse 存储和转换
May 27 Python
python购物车程序简单代码
Apr 18 Python
用Python编写一个简单的CS架构后门的方法
Nov 20 Python
Python数据可视化之画图
Jan 15 Python
python操作小程序云数据库实现简单的增删改查功能
Jun 06 Python
树莓派用python中的OpenCV输出USB摄像头画面
Jun 22 Python
如何关掉pycharm中的python console(图解)
Oct 31 Python
python opencv实现简易画图板
Aug 27 Python
人工智能深度学习OpenAI baselines的使用方法
May 20 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
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
2008/11/18 PHP
php中引用符号(&)的使用详解
2013/11/13 PHP
可以保证单词完整性的PHP英文字符串截取代码分享
2014/07/15 PHP
php实现cookie加密的方法
2015/03/10 PHP
php安装扩展mysqli的实现步骤及报错解决办法
2017/09/23 PHP
javascript中的prototype属性实例分析说明
2010/08/09 Javascript
脚本合并提升javascript性能示例
2014/02/24 Javascript
JavaScript匿名函数之模仿块级作用域
2015/12/12 Javascript
JS中的phototype详解
2017/02/04 Javascript
angularjs实现搜索的关键字在正文中高亮出来
2017/06/13 Javascript
利用require.js与angular搭建spa应用的方法实例
2017/07/19 Javascript
浅谈Vue.js 组件中的v-on绑定自定义事件理解
2017/11/17 Javascript
javascript实现循环广告条效果
2017/12/12 Javascript
vue中动态设置meta标签和title标签的方法
2018/07/11 Javascript
在Linux下调试Python代码的各种方法
2015/04/17 Python
Python 加密的实例详解
2017/10/09 Python
Python求一批字符串的最长公共前缀算法示例
2019/03/02 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
2020/01/02 Python
Python selenium使用autoIT上传附件过程详解
2020/05/26 Python
详解torch.Tensor的4种乘法
2020/09/03 Python
Python3+Flask安装使用教程详解
2021/02/16 Python
Python爬虫制作翻译程序的示例代码
2021/02/22 Python
SVG实现多彩圆环倒计时效果的示例代码
2017/11/21 HTML / CSS
德国在线购买葡萄酒网站:Geile Weine
2019/09/24 全球购物
自动化系在校本科生求职信
2013/10/23 职场文书
校友会欢迎辞
2014/01/13 职场文书
省级四好少年事迹材料
2014/01/25 职场文书
教师自查自纠材料
2014/10/14 职场文书
2014年幼儿园后勤工作总结
2014/11/10 职场文书
音乐课外活动总结
2015/05/09 职场文书
中秋节随笔
2015/08/15 职场文书
妇产科护理心得体会
2016/01/22 职场文书
2016年社区综治宣传月活动总结
2016/03/16 职场文书
使用springMVC所需要的pom配置
2021/09/15 Java/Android
关于Spring配置文件加载方式变化引发的异常详解
2022/01/18 Java/Android
Vue提供的三种调试方式你知道吗
2022/01/18 Vue.js