Python制作9行最简单音乐播放器?不,我不满足
人生苦短 我用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()
但是我不会满足哒~大家也不会满足的对吧 ~ ~ ~
所以给大家准备了有界面的音乐播放器
知识点 + 所需模块
1.python基础知识
2.requests库
3.time
4.pygame
5.tkinter
6.线程
环境
- windows
- pycharm 2021.2
- python 3.8
完整代码,运行即可,记得三连???
(直接明示了)
import os
import time
import tkinter
import tkinter.filedialog
import threading
import pygame
root = tkinter.Tk()
root.title('音乐播放器')
root.geometry('460x600+500+100')
root.resizable(False,False)
folder =''
res = []
num = 0
now_music = ''
def buttonChooseClick():
"""
添加文件夹
:return:
"""
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'
# python学习交流裙:660193417#
pause_resume.set('播放')
def play():
"""
播放音乐
:return:
"""
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))
def buttonPlayClick():
"""
点击播放
:return:
"""
buttonNext['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.music.pause()
pause_resume.set('继续')
elif pause_resume.get() == '继续':
pygame.mixer.music.unpause()
pause_resume.set('暂停')
def buttonStopClick():
"""
停止播放
:return:
"""
global playing
playing = False
pygame.mixer.music.stop()
def buttonNextClick():
"""
下一首
:return:
"""
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():
"""
关闭窗口
:return:
"""
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):
"""
声音控制
:param value: 0.0-1.0
:return:
"""
pygame.mixer.music.set_volume(float(value))
def buttonPrevClick():
"""
上一首
:return:
"""
global playing
playing = False
pygame.mixer.music.stop()
global num
if num == 0:
num = len(res) - 2
elif num == len(res) - 1:
num -= 2
else:
num -= 2
print(num)
playing = True
t = threading.Thread(target=play)
t.start()
root.protocol('WM_DELETE_WINDOW', closeWindow)
buttonChoose = tkinter.Button(root,text='添加',command=buttonChooseClick)
buttonChoose.place(x=50,y=10,width=50,height=20)
pause_resume = tkinter.StringVar(root,value='播放')
buttonPlay = tkinter.Button(root,textvariable=pause_resume,command=buttonPlayClick)
buttonPlay.place(x=190,y=10,width=50,height=20)
buttonPlay['state'] = 'disabled'
buttonStop = tkinter.Button(root, text='停止',command=buttonStopClick)
buttonStop.place(x=120, y=10, width=50, height=20)
buttonStop['state'] = 'disabled'
buttonNext = tkinter.Button(root, text='下一首',command=buttonNextClick)
buttonNext.place(x=260, y=10, width=50, height=20)
buttonNext['state'] = 'disabled'
buttonPrev = tkinter.Button(root, text='上一首',command=buttonPrevClick)
buttonPrev.place(x=330, y=10, width=50, height=20)
buttonPrev['state'] = 'disabled'
musicName = tkinter.StringVar(root, value='暂时没有播放音乐...')
labelName = tkinter.Label(root, textvariable=musicName)
labelName.place(x=10, y=30, width=260, height=20)
s = tkinter.Scale(root, label='音量', from_=0, to=1, orient=tkinter.HORIZONTAL,
length=240, showvalue=0, tickinterval=2, resolution=0.1,command=control_voice)
s.place(x=50, y=50, width=200)
root.mainloop()