您好,登錄后才能下訂單哦!
本篇內容主要講解“基于Python怎么實現音樂播放器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于Python怎么實現音樂播放器”吧!
第一步引入必須的各類包
import os import tkinter import tkinter.filedialog import random import time import threading import pygame
特別是pygame需要手動安裝
pip install pygame
首先選擇音樂目錄,然后創建播放現成,播放音樂。
# 播放按鈕 def buttonPlayClick(): # 選擇要播放的音樂文件夾 global folder if not folder: folder = tkinter.filedialog.askdirectory() if not folder: return global playing playing = True # 創建一個線程來播放音樂,當前主線程用來接收用戶操作 t = threading.Thread(target=play) t.start() # 根據情況禁用和啟用相應的按鈕 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'
這個好理解,也是直接現成操作,加個判斷,狀態更改。
# 暫停與恢復,兩個功能共用一個按鈕 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')
先結束現成,然后關閉窗體。
# 關閉程序時執行的代碼 def closeWindow(): # 修改變量,結束線程中的循環 global playing playing = False time.sleep(0.3) try: # 停止播放,如果已停止, # 再次停止時會拋出異常,所以放在異常處理結構中 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'))] # 初始化混音器設備 pygame.mixer.init() while playing: if not pygame.mixer.music.get_busy(): # 隨機播放一首歌曲 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) # 關閉程序時執行的代碼 def closeWindow(): # 修改變量,結束線程中的循環 global playing playing = False time.sleep(0.3) try: # 停止播放,如果已停止, # 再次停止時會拋出異常,所以放在異常處理結構中 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 # 創建一個線程來播放音樂,當前主線程用來接收用戶操作 t = threading.Thread(target=play) t.start() # 根據情況禁用和啟用相應的按鈕 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' # 暫停與恢復,兩個功能共用一個按鈕 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__': # 啟動消息循環 root.mainloop()
到此,相信大家對“基于Python怎么實現音樂播放器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。