时间:2022-07-19 10:17:53 | 栏目:Python代码 | 点击:次
audio_player = Tk() audio_player.title('Audio Player v1.0') audio_player.geometry('100x100+570+200') audio_player.maxsize(height=110, width=220) audio_player.minsize(height=110, width=220)
def selectFile(): file = filedialog.askopenfile(mode='r', filetypes=[('AudioFile', '*.mp3')]) global filePath filePath = str(file).split("'")[1] try: playAudio() except: pass
def changeText(text): if text == 'play': return 'pause' if text == 'pause': return 'play' def playStop(): playBtn.config(text=changeText(playBtn.config('text')[4])) if playBtn.config('text')[4] == 'pause': mixer.music.unpause() else: if playBtn.config('text')[4] == 'play': mixer.music.pause()
这里可以定义一个全局变量x,初始化为值0.5。
def audioINC(y): mixer.music.set_volume(y + 0.1) global x x += 0.1 def audioDEC(y): mixer.music.set_volume(y - 0.1) global x x -= 0.1
def playAudio(): try: mixer.init() mixer.music.load(filePath) mixer.music.set_volume(x) playBtn.config(text='pause') mixer.music.play() except: pass
frame = Frame(app) frame.place(x=35, y=20) openBtn = Button(frame, text='OpenFile', command=selectFile, width=8).grid(row=0, column=1) audioDec = Button(frame, text='?', command=lambda: audioDEC(x)).grid(row=1, column=0) playBtn = Button(frame, text='...', command=playStop, width=8) playBtn.grid(row=1, column=1) audioInc = Button(frame, text='?', command=lambda: audioINC(x)).grid(row=1, column=2) restartBtn = Button(frame, text='Restart', command=playAudio, width=8).grid(row=2, column=1) app.mainloop()
OpenFile
”按钮可以打开本地音频文件Restart
"按钮可以重新播放当前选中的音频本文仅仅是实现了一个简易的音频播放器,UI极其简陋,为了仅仅是实现音频播放的功能,仅供学习参考。