python使用PyGame播放Midi和Mp3文件的方法
更新時(shí)間:2015年04月24日 15:08:32 作者:feiwen
這篇文章主要介紹了python使用PyGame播放Midi和Mp3文件的方法,涉及Python操作多媒體文件的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了python使用PyGame播放Midi和Mp3文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
''' 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
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python數(shù)據(jù)結(jié)構(gòu)之二叉樹的遍歷實(shí)例
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹的遞歸遍歷實(shí)例,需要的朋友可以參考下2014-04-04
Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測試誤差實(shí)例
這篇文章主要介紹了Keras在訓(xùn)練期間可視化訓(xùn)練誤差和測試誤差實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
TensorFlow的自動(dòng)求導(dǎo)原理分析
這篇文章主要介紹了TensorFlow的自動(dòng)求導(dǎo)原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python實(shí)現(xiàn)Oracle查詢分組的方法示例
這篇文章主要介紹了python實(shí)現(xiàn)Oracle查詢分組的方法,結(jié)合實(shí)例形式分析了python使用group by子句及having子句實(shí)現(xiàn)Oracle查詢分組的相關(guān)操作技巧,需要的朋友可以參考下2020-04-04
Python實(shí)現(xiàn)將Word表格嵌入到Excel中
把Word中的表格轉(zhuǎn)到Excel中,順便做一個(gè)調(diào)整。這個(gè)需求在實(shí)際工作中,很多人還是經(jīng)常碰到的!本文就將介紹如何利用Python實(shí)現(xiàn)這一功能,需要的朋友可以了解一下2021-12-12

