基于Python實現(xiàn)音樂播放器的實現(xiàn)示例代碼
一、環(huán)境設(shè)置
第一步引入必須的各類包
import os import tkinter import tkinter.filedialog import random import time import threading import pygame
特別是pygame需要手動安裝
pip install pygame
二、播放功能
首先選擇音樂目錄,然后創(chuàng)建播放現(xiàn)成,播放音樂。
# 播放按鈕 def buttonPlayClick(): # 選擇要播放的音樂文件夾 global folder if not folder: folder = tkinter.filedialog.askdirectory() if not folder: return global playing playing = True # 創(chuàng)建一個線程來播放音樂,當(dāng)前主線程用來接收用戶操作 t = threading.Thread(target=play) t.start() # 根據(jù)情況禁用和啟用相應(yīng)的按鈕 buttonPlay['state'] = 'disabled' buttonStop['state'] = 'normal' buttonPause['state'] = 'normal' buttonNext['state'] = 'normal' pause_resume.set('Pause') buttonPlay = tkinter.Button(root, text='Play', command=buttonPlayClick) buttonPlay.place(x=20, y=10, width=50, height=20)
三、停止功能
很直接,直接停止播放,當(dāng)然更改按鈕狀態(tài)也屬常規(guī)操作。
def buttonStopClick(): global playing playing = False pygame.mixer.music.stop() musicName.set('暫時沒有播放音樂') buttonPlay['state'] = 'normal' buttonStop['state'] = 'disabled' buttonPause['state'] = 'disabled' buttonNext['state'] = 'disabled' global folder folder = '' buttonStop = tkinter.Button(root, text='Stop', command=buttonStopClick) buttonStop.place(x=80, y=10, width=50, height=20) buttonStop['state'] = 'disabled'
四、暫停與恢復(fù)
這個好理解,也是直接現(xiàn)成操作,加個判斷,狀態(tài)更改。
# 暫停與恢復(fù),兩個功能共用一個按鈕 def buttonPauseClick(): # global playing if pause_resume.get() == 'Pause': pygame.mixer.music.pause() pause_resume.set('Resume') elif pause_resume.get() == 'Resume': pygame.mixer.music.unpause() pause_resume.set('Pause')
五、關(guān)閉
先結(jié)束現(xiàn)成,然后關(guān)閉窗體。
# 關(guān)閉程序時執(zhí)行的代碼 def closeWindow(): # 修改變量,結(jié)束線程中的循環(huán) global playing playing = False time.sleep(0.3) try: # 停止播放,如果已停止, # 再次停止時會拋出異常,所以放在異常處理結(jié)構(gòu)中 pygame.mixer.music.stop() pygame.mixer.quit() except: pass root.destroy()
六、完整代碼
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:livingbody @file:music_play.py @time:2022/04/19 """ import os import tkinter import tkinter.filedialog import random import time import threading import pygame folder = '' def play(): # folder用來表示存放MP3音樂文件的文件夾 global folder musics = [folder + '\' + music for music in os.listdir(folder) \ if music.endswith(('.mp3', '.wav', '.ogg'))] # 初始化混音器設(shè)備 pygame.mixer.init() while playing: if not pygame.mixer.music.get_busy(): # 隨機(jī)播放一首歌曲 nextMusic = random.choice(musics) musicName.set('playing....' + nextMusic) pygame.mixer.music.load(nextMusic.encode()) # 播放一次 pygame.mixer.music.play(1) else: time.sleep(0.3) root = tkinter.Tk() root.title('音樂播放器v1.0') root.geometry('280x70+400+300') root.resizable(False, False) # 關(guān)閉程序時執(zhí)行的代碼 def closeWindow(): # 修改變量,結(jié)束線程中的循環(huán) global playing playing = False time.sleep(0.3) try: # 停止播放,如果已停止, # 再次停止時會拋出異常,所以放在異常處理結(jié)構(gòu)中 pygame.mixer.music.stop() pygame.mixer.quit() except: pass root.destroy() root.protocol('WM_DELETE_WINDOW', closeWindow) pause_resume = tkinter.StringVar(root, value='NotSet') playing = False # 播放按鈕 def buttonPlayClick(): # 選擇要播放的音樂文件夾 global folder if not folder: folder = tkinter.filedialog.askdirectory() if not folder: return global playing playing = True # 創(chuàng)建一個線程來播放音樂,當(dāng)前主線程用來接收用戶操作 t = threading.Thread(target=play) t.start() # 根據(jù)情況禁用和啟用相應(yīng)的按鈕 buttonPlay['state'] = 'disabled' buttonStop['state'] = 'normal' buttonPause['state'] = 'normal' buttonNext['state'] = 'normal' pause_resume.set('Pause') buttonPlay = tkinter.Button(root, text='Play', command=buttonPlayClick) buttonPlay.place(x=20, y=10, width=50, height=20) # 停止按鈕 def buttonStopClick(): global playing playing = False pygame.mixer.music.stop() musicName.set('暫時沒有播放音樂') buttonPlay['state'] = 'normal' buttonStop['state'] = 'disabled' buttonPause['state'] = 'disabled' buttonNext['state'] = 'disabled' global folder folder = '' buttonStop = tkinter.Button(root, text='Stop', command=buttonStopClick) buttonStop.place(x=80, y=10, width=50, height=20) buttonStop['state'] = 'disabled' # 暫停與恢復(fù),兩個功能共用一個按鈕 def buttonPauseClick(): # global playing if pause_resume.get() == 'Pause': pygame.mixer.music.pause() pause_resume.set('Resume') elif pause_resume.get() == 'Resume': pygame.mixer.music.unpause() pause_resume.set('Pause') buttonPause = tkinter.Button(root, textvariable=pause_resume, command=buttonPauseClick) buttonPause.place(x=140, y=10, width=50, height=20) buttonPause['state'] = 'disabled' # 下一首音樂 def buttonNextClick(): global playing playing = False pygame.mixer.music.stop() pygame.mixer.quit() buttonPlayClick() buttonNext = tkinter.Button(root, text='Next', command=buttonNextClick) buttonNext.place(x=200, y=10, width=50, height=20) buttonNext['state'] = 'disabled' musicName = tkinter.StringVar(root, value='暫時沒有播放音樂...') labelName = tkinter.Label(root, textvariable=musicName) labelName.place(x=0, y=40, width=270, height=20) if __name__ == '__main__': # 啟動消息循環(huán) root.mainloop()
七、改進(jìn)
下一步,可加入播放列表,點選播放音樂,也可以拖動進(jìn)度播放。
以上就是基于Python實現(xiàn)音樂播放器的實現(xiàn)示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python音樂播放器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實現(xiàn)學(xué)生管理系統(tǒng)的完整代碼(面向?qū)ο?
這篇文章主要介紹了Python實現(xiàn)學(xué)生管理系統(tǒng)的完整代碼(面向?qū)ο?,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04python?Ajenti控制面板輕松地管理所有服務(wù)器網(wǎng)站
Ajenti是一個值得擁有的管理面板,免費開源的管理面板工具,可以幫助你集中管理多個服務(wù)器和網(wǎng)站,Ajenti?支持?Linux、BSD、Mac?OS?X和Windows?等多個操作系統(tǒng),并且可以通過一個直觀的?Web?界面來完成各種系統(tǒng)管理任務(wù)2024-01-01解決Python內(nèi)層for循環(huán)如何break出外層的循環(huán)的問題
今天小編就為大家分享一篇解決Python內(nèi)層for循環(huán)如何break出外層的循環(huán)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python項目導(dǎo)入open3d后報錯ImportError:DLL load failed:找不到
這篇文章主要介紹了python項目導(dǎo)入open3d后報錯ImportError:DLL load failed:找不到指定的模塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Python二進(jìn)制串轉(zhuǎn)換為通用字符串的方法
今天小編就為大家分享一篇Python二進(jìn)制串轉(zhuǎn)換為通用字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Tensorflow加載模型實現(xiàn)圖像分類識別流程詳解
在視覺領(lǐng)域可以分為:1、圖像分類 2、語義分割 3、實例分割 4、目標(biāo)檢測(跟蹤) 5、關(guān)鍵點檢測。該篇主要講解利用Tensorflow 對圖像進(jìn)行圖像分類2022-09-09Python中subplots_adjust函數(shù)的用法
這篇文章主要介紹了Python中subplots_adjust函數(shù)的用法及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08