我的原文鏈接:
https://blog.csdn.net/m0_72282564/article/details/129215308
前言
嗨嘍,大家好呀~這里是愛看美女的茜茜吶

就是用Python做一個簡易的音樂播放器,廢話不多說,咱們直接開干

當然,今天做這個肯定不是最簡單的,最簡單的音樂播放器,9行代碼足以
簡易播放器
代碼展示
導入模塊
import time
import pygame
file = r'歌曲路徑'
pygame.mixer.init()
print('正在播放',file)
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
time.sleep(130)
pygame.mixer.music.stop()
效果展示

多功能播放器
知識點和所需模塊
python基礎(chǔ)知識
requests庫
time
pygame
tkinter
線程
如果安裝python第三方模塊:
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
在pycharm中點擊Terminal(終端) 輸入安裝命令
環(huán)境
windows
pycharm 2021.2
python 3.8
代碼展示
導入模塊
import os
import time
import tkinter
import tkinter.filedialog
import threading
import pygame # pip
root = tkinter.Tk()
root.title('音樂播放器')
root.geometry('460x600+500+100')
root.resizable(False,False) # 不能拉伸
folder =''
res = []
num = 0
now_music = ''
添加文件夾
def buttonChooseClick():
global folder
global res
if not folder:
folder = tkinter.filedialog.askdirectory()
musics = [folder + '\\' + music
for music in os.listdir(folder) \
\
if music.endswith(('.mp3','.wav','.ogg'))]
ret = []
for i in musics:
ret.append(i.split('\\')[1:])
res.append(i.replace('\\','/'))
var2 = tkinter.StringVar()
var2.set(ret)
lb = tkinter.Listbox(root,listvariable=var2)
lb.place(x=50,y=100,width=260,height=300)
if not folder:
return
global playing
playing = True
根據(jù)情況禁用和啟用相應的按鈕
buttonPlay['state'] = 'normal'
buttonStop['state'] = 'normal'
# buttonPause['state'] = 'normal'
pause_resume.set('播放')
播放音樂
def play():
if len(res):
pygame.mixer.init()
global num
while playing:
if not pygame.mixer.music.get_busy():
netxMusic = res[num]
print(netxMusic)
print(num)
pygame.mixer.music.load(netxMusic.encode())
# 播放
pygame.mixer.music.play(1)
if len(res) -1 == num:
num = 0
else:
num = num + 1
netxMusic = netxMusic.split('\\')[1:]
musicName.set('playing......' + ''.join(netxMusic))
else:
time.sleep(0.1)
點擊播放
def buttonPlayClick():
buttonNext['state'] = 'normal'
buttonPrev['state'] = 'normal'
選擇要播放的音樂文件夾
if pause_resume.get() == '播放':
pause_resume.set('暫停')
global folder
if not folder:
folder = tkinter.filedialog.askdirectory()
if not folder:
return
global playing
playing = True
創(chuàng)建一個線程來播放音樂,當前主線程用來接收用戶操作
t = threading.Thread(target=play)
t.start()
elif pause_resume.get() == '暫停':
# pygame.mixer.init()
pygame.mixer.music.pause()
pause_resume.set('繼續(xù)')
elif pause_resume.get() == '繼續(xù)':
# pygame.mixer.init()
pygame.mixer.music.unpause()
pause_resume.set('暫停')
停止播放
def buttonStopClick():
global playing
playing = False
pygame.mixer.music.stop()
下一首
def buttonNextClick():
global playing
playing = False
pygame.mixer.music.stop()
global num
if len(res) == num:
num = 0
playing = True
創(chuàng)建線程播放音樂,主線程用來接收用戶操作
t = threading.Thread(target=play)
t.start()
關(guān)閉窗口
def closeWindow():
修改變量,結(jié)束線程中的循環(huán)
global playing
playing = False
time.sleep(0.3)
停止播放,如果已停止,
再次停止時會拋出異常,所以放在異常處理結(jié)構(gòu)中
try:
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
pass
root.destroy()
聲音控制
def control_voice(value=0.5):
pygame.mixer.music.set_volume(float(value))
上一首
def buttonPrevClick():
global playing
playing = False
pygame.mixer.music.stop()
#
# pygame.mixer.quit()
global num
# num += 1
# num -= 1
if num == 0:
num = len(res) - 2
# num -= 1
elif num == len(res) - 1:
num -= 2
else:
num -= 2
# num -= 1
print(num)
playing = True
創(chuàng)建一個線程來播放音樂,當前主線程用來接收用戶操作
t = threading.Thread(target=play)
t.start()
窗口關(guān)閉
root.protocol('WM_DELETE_WINDOW', closeWindow)
效果展示



尾語
感謝你觀看我的文章吶~本次航班到這里就結(jié)束啦 ??
希望本篇文章有對你帶來幫助 ??,有學習到一點知識~
躲起來的星星??也在努力發(fā)光,你也要努力加油(讓我們一起努力叭)。