我的原文链接:
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基础知识
requests库
time
pygame
tkinter
线程
如果安装python第三方模块:
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
在pycharm中点击Terminal(终端) 输入安装命令
环境
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
根据情况禁用和启用相应的按钮
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
创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
elif pause_resume.get() == '暂停':
# pygame.mixer.init()
pygame.mixer.music.pause()
pause_resume.set('继续')
elif pause_resume.get() == '继续':
# 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
创建线程播放音乐,主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
关闭窗口
def closeWindow():
修改变量,结束线程中的循环
global playing
playing = False
time.sleep(0.3)
停止播放,如果已停止,
再次停止时会抛出异常,所以放在异常处理结构中
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
创建一个线程来播放音乐,当前主线程用来接收用户操作
t = threading.Thread(target=play)
t.start()
窗口关闭
root.protocol('WM_DELETE_WINDOW', closeWindow)
效果展示
尾语
感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。