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 从远程服务器下载日志文件的程序
Feb 10 Python
使用Python操作MySQL的一些基本方法
Aug 16 Python
Python的Twisted框架上手前所必须了解的异步编程思想
May 25 Python
详解python中@的用法
Mar 27 Python
Python实现操纵控制windows注册表的方法分析
May 24 Python
python pandas生成时间列表
Jun 29 Python
Python+OpenCv制作证件图片生成器的操作方法
Aug 21 Python
Python中常用的高阶函数实例详解
Feb 21 Python
Python实现的北京积分落户数据分析示例
Mar 27 Python
Python3爬虫中识别图形验证码的实例讲解
Jul 30 Python
Jupyter notebook 更改文件打开的默认路径操作
May 21 Python
Python中with上下文管理协议的作用及用法
Mar 18 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中this关键字用法分析
2016/12/07 PHP
Laravel框架表单验证操作实例分析
2019/09/30 PHP
html5+javascript实现简单上传的注意细节
2016/04/18 Javascript
PassWord输入框代码分享
2016/06/07 Javascript
模仿password输入框的实现代码
2016/06/07 Javascript
每日十条JavaScript经验技巧(二)
2016/06/23 Javascript
parabola.js抛物线与加入购物车效果的示例代码
2017/10/25 Javascript
Vue.js下拉菜单组件使用方法详解
2019/10/19 Javascript
vue中v-model对select的绑定操作
2020/08/31 Javascript
node.js如何根据URL返回指定的图片详解
2020/10/21 Javascript
js加减乘除精确运算方法实例代码
2021/01/17 Javascript
vue3.0中使用element的完整步骤
2021/03/04 Vue.js
[01:32:50]DOTA2-DPC中国联赛 正赛 DLG vs XG BO3 第一场 1月25日
2021/03/11 DOTA
Python对象的深拷贝和浅拷贝详解
2014/08/25 Python
零基础写python爬虫之urllib2使用指南
2014/11/05 Python
Python实现设置windows桌面壁纸代码分享
2015/03/28 Python
仅用50行Python代码实现一个简单的代理服务器
2015/04/08 Python
复习Python中的字符串知识点
2015/04/14 Python
python简单实现基数排序算法
2015/05/16 Python
python基础知识小结之集合
2015/11/25 Python
Python正则表达式使用经典实例
2016/06/21 Python
python分布式编程实现过程解析
2019/11/08 Python
python爬虫scrapy框架之增量式爬虫的示例代码
2021/02/26 Python
分解成质因数(如435234=251*17*17*3*2,据说是华为笔试题)
2014/07/16 面试题
中学教师实习自我鉴定
2013/09/28 职场文书
初三家长会邀请函
2014/01/18 职场文书
运动会入场解说词
2014/02/07 职场文书
应聘编辑自荐信范文
2014/03/12 职场文书
市场策划求职信
2014/08/07 职场文书
委托书如何写
2014/08/30 职场文书
未受刑事制裁公证证明
2014/09/20 职场文书
2015年教育实习工作总结
2015/04/24 职场文书
2015社区个人工作总结范文
2015/05/13 职场文书
创业计划书之青年旅馆
2019/09/23 职场文书
vue项目两种方式实现竖向表格的思路分析
2021/04/28 Vue.js
python DataFrame中stack()方法、unstack()方法和pivot()方法浅析
2022/04/06 Python