对于音频的操作可以使用pygame包中的sound 和 music对象进行音乐播放。
Sound对象适合处理较短的音乐,如OGG和WAV格式的音频文件。
Music对象出来可以播放OGG、WAV音频外,还可以播放mp3格式。
from pygame import mixer #mixer对象可以播放音乐
一、Sound 对象
mixer.init() 先初始化
创建Sound对象
sound = mixer.Sound(音频文件名)
sound.play(loops = 0) 播放音乐,loops代表播放次数,默认为0,表示播放一次;loops=5可播放6次;loops=-1 可重复播放
sound.stop() 结束播放
sound.set_volume(value) 设置音量,音量有最低到高0.0--1.0
sound.get_volume() 获取当前的音量
二、music对象
mixer.init() 先初始化
mixer.music.load(filename) 停止正在播放的音乐,filename为歌曲的文件名
mixer.music.play(loops=0,start=0.0) 播放歌曲,loops表示播放次数,默认为0,播放1次;loops=5可以播放6次;loop=-1可以重复播放
mixer.music.stop() 停止播放
mixer.music.pause() 暂停播放
mixer.music.unpause() 用pause()暂停后,必须使用这个函数来继续播放
mixer.music.set_volume(value) 设置音量,最大1.0,最小0.0
mixer.music.get_volume() 获取当前音量
mixer.music.get_busy() 检查歌曲播放状态,True为正在播,False为不在播
from pygame import mixer
import glob
import tkinter as tk mixer.init()
win = tk.Tk()
win.geometry("640x380")
win.title("mp3 播放器") labeltitle = tk.Label(win,text='mp3 播放器',fg = 'red')
labeltitle.pack() framel = tk.Frame(win)
framel.pack() musicList = glob.glob('*.mp3') playSong = preplaySone = ''
index = 0
volume = 0.6
choice = tk.StringVar() def choose():
global playSong
msg.set("播放歌曲:"+choice.get())
playSong = choice.get() def playMp3():
global status,playSong,preplaySone
if playSong == preplaySone:
if not mixer.music.get_busy():
mixer.music.load(playSong)
mixer.music.play(loops = -1)
else:
mixer.music.pause()
else:
playNewSong()
preplaySone = playSong def playNewSong():
global playSong
mixer.music.stop()
mixer.music.load(playSong)
mixer.music.play(loops = -1)
msg.set("正在播放:{}".format(playSong)) def pauseMp3():
mixer.music.pause()
msg.set("暂停歌曲:{}".format(playSong)) def increase():
global volume
volume += 0.1
if volume >=1:
volume = 1
mixer.music.set_volume(volume) def decrease():
global volume
volume -= 0.1
if volume <= 0.1:
volume = 0.1
mixer.music.set_volume(volume) def stopMp3():
mixer.music.stop()
msg.set("\n停止播放") def exitMp3():
mixer.music.stop()
win.destroy() for music in musicList:
rbtem = tk.Radiobutton(framel,text=music,variable = choice,value=music,command=choose)
if index == 0:
rbtem.select()
playSong = preplaySone = music
rbtem.grid(row = index,column = 0,sticky = 'w')
index += 1 msg = tk.StringVar()
msg.set("\n播放歌曲:"+playSong)
label = tk.Label(win,textvariable=msg,fg='blue')
label.pack() labelsep = tk.Label(win,text='\n')
labelsep.pack() frame2 = tk.Frame(win)
frame2.pack() button1 = tk.Button(frame2,text='播放',width=8,command = playMp3)
button1.grid(row=0,column=0,padx=5,pady=5) button2 = tk.Button(frame2,text='暂停',width=8,command = pauseMp3)
button2.grid(row=0,column=1,padx=5,pady=5) button3 = tk.Button(frame2,text='音量调大',width=8,command = increase)
button3.grid(row=0,column=2,padx=5,pady=5) button4 = tk.Button(frame2,text='音量调小',width=8,command = decrease)
button4.grid(row=0,column=3,padx=5,pady=5) button5 = tk.Button(frame2,text='停止',width=8,command = stopMp3)
button5.grid(row=0,column=4,padx=5,pady=5) button6 = tk.Button(frame2,text='结束',width=8,command = exitMp3)
button6.grid(row=0,column=5,padx=5,pady=5) win.protocol("WM_DELETE_WINDOW",exitMp3)
win.mainloop()
#print(musicList)