How to use Python to write Local Music player
This article mainly shows you "how to use Python to write a local music player", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to write a local music player using Python" this article.
Start with the complete code:
Modify the folder path to run
#-*-encoding: utf-8-* -'@ Description:: @ Date: 2022-03-24 17:43:26@Author: sudden rain @ version: 1.0 please import the related library file import osimport tkinterimport tkinter.filedialogimport randomimport timeimport threadingimport pygamefrom asyncio.base_tasks import _ task_print_stackfrom cProfile import labelfrom email import headerfrom functools import total_orderingfrom importlib import find_loaderfrom logging import rootfrom mimetypes import initfrom re import Xfrom turtle import widthfrom matplotlib. Pyplot import pausefrom scipy import rand# sets folder path folder = ritual C:\ Users\ Administrator\ Desktop\ Python\ Python Code\ TiquMusicFromVedio' "" @ description: music playback Default playback of all mp3 files in the folder-@ param:-@ Returns:-"" def play (): global folder music = [folder+'\\'+ music for music in os.listdir (folder)\ if music.endswith (('.mp3', '.wav') ] total = len (music) # initialize mixer device pygame.mixer.init () while playing: if not pygame.mixer.music.get_busy (): # Random play a song nextMusic = random.choice (music) pygame.mixer.music.load (nextMusic.encode ()) # play it once Pygame.mixer.music.play (1) musicName.set ('playing.'+nextMusic) else: time.sleep (0.3) root = tkinter.Tk () root.title (' music player') root.geometry ('700x80' 400') root.resizable (False False) # Code executed when closing the program 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# play button def buttonPlayClick (): global folder if not folder: folder = tkinter.filedialog.askdirectory () if not folder: return global playing playing = True # create a thread to play music t = threading.Thread (target=play) t.start () # disable or enable the appropriate button buttonPlay [' state'] = 'disabled' as appropriate ButtonStop ['state'] =' normal' buttonPause ['state'] =' normal' buttonNext ['state'] =' normal' pause_resume.set ('Pause') buttonPlay = tkinter.Button (root) Text=' Play',command=buttonPlayClick) buttonPlay.place # stop button def buttonStopClick (): global playing playing = False pygame.mixer.music.stop () musicName.set ('no music is playing') buttonPlay ['state'] =' normal' buttonStop ['state'] =' disabled' buttonPause ['state'] =' disabled'buttonStop = tkinter.Button (root,text='Stop',command=buttonStopClick) buttonStop.place Height=20) buttonStop ['state'] =' disabled'# pause and resume Multiplexing Button def buttonPauseClick (): global playing if pause_resume.get () = 'Pause': # playing = False pygame.mixer.music.pause () pause_resume.set (' Resume') elif pause_resume.get () = 'Resume': # playing = True pygame.mixer.music.unpause () pause _ resume.set ('Pause') buttonPause = tkinter.Button (root Textvariable=pause_resume,command=buttonPauseClick) buttonPause.place ['state'] =' disabled'# 's next def buttonNextClick (): global playing playing = False pygame.mixer.music.stop () pygame.mixer.quit () buttonPlayClick () buttonNext = tkinter.Button (root,text='Next',command=buttonNextClick) buttonNext.place (xzhong 200pcdfdwth50) buttonNext ['state'] =' disabled'musicName = tkinter.StringVar (root,value=' is not playing music for the moment!') LabelName = tkinter.Label (root,textvariable=musicName) labelName.place (Xerox 0pl 40m widthpiece 700pm heightening 20) # start the message loop root.mainloop ()
The running results are as follows:
The above is all the content of the article "how to write a local music player using Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!