• 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使用点操作符访问字典(dict)数据的方法
  • 深入分析在Python模块顶层运行的代码引起的一个Bug
  • python实现登陆知乎获得个人收藏并保存为word文件
  • Python实现对excel文件列表值进行统计的方法
  • 使用python提取html文件中的特定数据的实现代码
  • 跟老齐学Python之让人欢喜让人忧的迭代
  • python抓取网页时字符集转换问题处理方案分享
  • pytyon 带有重复的全排列
  • Python学习笔记整理3之输入输出、python eval函数

文章分类

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

最近更新的内容

    • Python中的Function定义方法第1/2页
    • Python中os和shutil模块实用方法集锦
    • Python库urllib与urllib2主要区别分析
    • Python编程中的文件操作攻略
    • Python实现计算文件夹下.h和.cpp文件的总行数
    • Python中第三方库Requests库的高级用法详解
    • Linux上安装Python的PIL和Pillow库处理图片的实例教程
    • Python中对列表排序实例
    • Python随机生成信用卡卡号的实现方法
    • 如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求

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

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