• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用PyGame播放Midi和Mp3文件的方法

python使用PyGame播放Midi和Mp3文件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python安装pygame,python pygame,python3.5 pygame,python3 pygame,python3.4 pygame等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python使用pygame模块编写俄罗斯方块游戏的代码实例
  • Python使用pygame模块编写俄罗斯方块游戏的代码实例
  • 用Python写飞机大战游戏之pygame入门(4):获取鼠标的位置及运动
  • python使用PyGame模块播放声音的方法
  • Python加pyGame实现的简单拼图游戏实例
  • python使用PyGame播放Midi和Mp3文件的方法
  • python使用PyGame绘制图像并保存为图片文件的方法
  • python使用PyGame播放Midi和Mp3文件的方法
  • python使用PyGame绘制图像并保存为图片文件的方法
  • python中pygame模块用法实例

相关文章

  • 总结Python编程中函数的使用要点
  • Python使用Redis实现作业调度系统(超简单)
  • Python基于pillow判断图片完整性的方法
  • Python 调用VC++的动态链接库(DLL)
  • Python实现子类调用父类的方法
  • 详解Swift中属性的声明与作用
  • 讲解Python中运算符使用时的优先级
  • python抓取网页内容示例分享
  • python计算最小优先级队列代码分享
  • python基础教程之简单入门说明(变量和控制语言使用方法)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Windows安装Python、pip、easy_install的方法
    • Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
    • 利用Python中unittest实现简单的单元测试实例详解
    • python中global与nonlocal比较
    • 举例讲解Python中的迭代器、生成器与列表解析用法
    • 教你安装python Django(图文)
    • 跟老齐学Python之Python文档
    • 详细解析Python中的变量的数据类型
    • python实现JAVA源代码从ANSI到UTF-8的批量转换方法
    • python使用正则表达式提取网页URL的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有