python使用PyGame播放Midi和Mp3文件的方法
更新時間:2015年04月24日 15:08:32 作者:feiwen
這篇文章主要介紹了python使用PyGame播放Midi和Mp3文件的方法,涉及Python操作多媒體文件的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了python使用PyGame播放Midi和Mp3文件的方法。分享給大家供大家參考。具體實現(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
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Django JWT Token RestfulAPI用戶認(rèn)證詳解
這篇文章主要介紹了Django JWT Token RestfulAPI用戶認(rèn)證詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01簡單了解Django ORM常用字段類型及參數(shù)配置
這篇文章主要介紹了簡單了解Django ORM常用字段類型及參數(shù)配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01在Python中字典根據(jù)多項規(guī)則排序的方法
今天小編就為大家分享一篇在Python中字典根據(jù)多項規(guī)則排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Pandas merge合并兩個DataFram的實現(xiàn)
本文主要介紹了Pandas merge合并兩個DataFram的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python利用pandas將excel文件轉(zhuǎn)換為txt文件的方法
今天小編就為大家分享一篇python利用pandas將excel文件轉(zhuǎn)換為txt文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python實現(xiàn)對csv文件的列的內(nèi)容讀取
今天小編就為大家分享一篇python實現(xiàn)對csv文件的列的內(nèi)容讀取,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07