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 多线程应用介绍
Dec 19 Python
深入解析Python中的list列表及其切片和迭代操作
Mar 13 Python
Python生成密码库功能示例
May 23 Python
python matplotlib中文显示参数设置解析
Dec 15 Python
Python中XlsxWriter模块简介与用法分析
Apr 24 Python
Python装饰器知识点补充
May 28 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
python操作excel的方法
Aug 16 Python
python中的数据结构比较
May 13 Python
python numpy 反转 reverse示例
Dec 04 Python
浅析Django中关于session的使用
Dec 30 Python
Pycharm pyuic5实现将ui文件转为py文件,让UI界面成功显示
Apr 08 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实现给定一列字符,生成指定长度的所有可能组合示例
2019/06/22 PHP
js菜单点击显示或隐藏效果的简单实例
2014/01/13 Javascript
struts2+jquery组合验证注册用户是否存在
2014/04/30 Javascript
jquery 获取 outerHtml 包含当前节点本身的代码
2014/10/30 Javascript
AngularJS基础 ng-include 指令简单示例
2016/08/01 Javascript
基于jQuery实现淡入淡出效果轮播图
2020/07/31 Javascript
Vue2.0 多 Tab切换组件的封装实例
2017/07/28 Javascript
JS Testing Properties 判断属性是否在对象里的方法
2017/10/01 Javascript
聊聊Vue.js的template编译的问题
2017/10/09 Javascript
Vue-cli Eslint在vscode里代码自动格式化的方法
2018/02/23 Javascript
vue 路由嵌套高亮问题的解决方法
2018/05/17 Javascript
js+canvas实现验证码功能
2020/09/21 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
使用异步controller与jQuery实现卷帘式分页
2019/06/18 jQuery
[49:11]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第二场 12.20
2020/12/23 DOTA
[01:36:17]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第一场 1月31日
2021/03/11 DOTA
python sqlobject(mysql)中文乱码解决方法
2008/11/14 Python
python模拟新浪微博登陆功能(新浪微博爬虫)
2013/12/24 Python
使用python实现递归版汉诺塔示例(汉诺塔递归算法)
2014/04/08 Python
python爬虫入门教程--正则表达式完全指南(五)
2017/05/25 Python
基于python框架Scrapy爬取自己的博客内容过程详解
2019/08/05 Python
python实现超级玛丽游戏
2020/03/18 Python
2020新版本pycharm+anaconda+opencv+pyqt环境配置学习笔记,亲测可用
2020/03/24 Python
Python logging模块handlers用法详解
2020/08/14 Python
html5文字阴影效果text-shadow使用示例
2013/07/25 HTML / CSS
html5视频媒体标签video的使用方法及完整参数说明详解
2019/09/27 HTML / CSS
一个基于canvas的移动端图片编辑器的实现
2020/10/28 HTML / CSS
《月光启蒙》教学反思
2014/03/01 职场文书
初中同学会活动方案
2014/08/22 职场文书
党支部反对四风思想汇报
2014/10/10 职场文书
工作时间擅自离岗检讨书
2014/10/24 职场文书
2015年网络管理员工作总结
2015/05/21 职场文书
践行三严三实心得体会(2016推荐篇)
2016/01/06 职场文书
关于做家务的心得体会
2016/01/23 职场文书
NodeJs使用webpack打包项目的方法详解
2022/02/28 NodeJs
JavaScript圣杯布局与双飞翼布局实现案例详解
2022/08/05 Javascript