完整代码:
修改文件夹路径即可运行
'''
@Description: :
@Date :2022/03/24 17:43:26
@Author :骤&雨
@version :1.0
'''
import os
import tkinter
import tkinter.filedialog
import random
import time
import threading
import pygame
from asyncio.base_tasks import _task_print_stack
from cProfile import label
from email import header
from functools import total_ordering
from importlib import find_loader
from logging import root
from mimetypes import init
from re import X
from turtle import width
from matplotlib.pyplot import pause
from scipy import rand
folder = r'C:\Users\Administrator\Desktop\Python\Python Code\TiquMusicFromVedio'
"""
@description : 音乐播放,默认播放文件夹内的所有mp3文件
---------
@param :
-------
@Returns :
-------
"""
def play():
global folder
music =[folder+'\\'+music for music in os.listdir(folder)\
if music.endswith(('.mp3','.wav','.ogg'))]
total = len(music)
pygame.mixer.init()
while playing:
if not pygame.mixer.music.get_busy():
nextMusic = random.choice(music)
pygame.mixer.music.load(nextMusic.encode())
pygame.mixer.music.play(1)
musicName.set('playing.......'+nextMusic)
else:
time.sleep(0.3)
root = tkinter.Tk()
root.title('音乐播放器')
root.geometry('700x80+400+300')
root.resizable(False,False)
def closeWindow():
global playing
playing = False
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'
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=700,height=20)
root.mainloop()
运行结果如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f9e34a53a839fda89cb5c62d042975af.png)