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 相关文章推荐
Windows下实现Python2和Python3两个版共存的方法
Jun 12 Python
Python 实现简单的电话本功能
Aug 09 Python
python入门前的第一课 python怎样入门
Mar 06 Python
Python按钮的响应事件详解
Mar 04 Python
Python read函数按字节(字符)读取文件的实现
Jul 03 Python
Python实现串口通信(pyserial)过程解析
Sep 25 Python
pandas 中对特征进行硬编码和onehot编码的实现
Dec 20 Python
Python如何执行精确的浮点数运算
Jul 31 Python
python实现二分查找算法
Sep 18 Python
python pymysql库的常用操作
Oct 16 Python
详解Python自动化之文件自动化处理
Jun 21 Python
移除Selenium中window.navigator.webdriver值
Jun 10 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 URL编码解码函数代码
2009/03/10 PHP
php ios推送(代码)
2013/07/01 PHP
摘自织梦CMS的HTTP文件下载类
2015/08/08 PHP
详解PHP的Yii框架的运行机制及其路由功能
2016/03/17 PHP
PHP依赖注入原理与用法分析
2018/08/21 PHP
关于php开启错误提示的总结
2019/09/24 PHP
javascript学习笔记(九) js对象 设计模式
2012/06/19 Javascript
jQuery设置与获取HTML,文本和值的简单实例
2014/02/26 Javascript
Jquery插件实现点击获取验证码后60秒内禁止重新获取
2015/03/13 Javascript
jquery插件pagination实现无刷新ajax分页
2015/09/30 Javascript
jquery 表单验证之通过 class验证表单不为空
2015/11/02 Javascript
AngularJS ng-app 指令实例详解
2016/07/30 Javascript
jQuery选择器总结之常用元素查找方法
2016/08/04 Javascript
javascript实现一个网页加载进度loading
2017/01/04 Javascript
浅谈在fetch方法中添加header后遇到的预检请求问题
2017/08/31 Javascript
详解webpack提取第三方库的正确姿势
2017/12/22 Javascript
Vue.js实现双向数据绑定方法(表单自动赋值、表单自动取值)
2018/08/27 Javascript
小程序文字跑马灯效果
2018/12/28 Javascript
js实现页面多个日期时间倒计时效果
2019/06/20 Javascript
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
javascript解析json格式的数据方法详解
2020/08/07 Javascript
[02:39]DOTA2英雄基础教程 极限穿梭编织者
2013/12/05 DOTA
[07:54]DOTA2-DPC中国联赛 正赛 iG vs VG 选手采访
2021/03/11 DOTA
Python实现的数据结构与算法之基本搜索详解
2015/04/22 Python
python控制台中实现进度条功能
2015/11/10 Python
python判断文件夹内是否存在指定后缀文件的实例
2019/06/10 Python
python pyinstaller 加载ui路径方法
2019/06/10 Python
Django2 连接MySQL及model测试实例分析
2019/12/10 Python
Python多线程通信queue队列用法实例分析
2020/03/24 Python
解决启动django,浏览器显示“服务器拒绝访问”的问题
2020/05/13 Python
技校生自我鉴定
2013/12/08 职场文书
社区母亲节活动记录
2014/03/06 职场文书
构建和谐校园倡议书
2015/01/19 职场文书
自主招生自荐信格式
2015/03/04 职场文书
秋收起义观后感
2015/06/11 职场文书
2016高校自主招生自荐信范文
2016/01/28 职场文书