Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Music player based on Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to implement a music player based on Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement a music player based on Python".

I. Environmental settings

The first step is to introduce the necessary packages

Import osimport tkinterimport tkinter.filedialogimport randomimport timeimport threadingimport pygame

In particular, pygame needs to be installed manually

Pip install pygame II. Playback function

First select the music catalog, and then create a ready-made playback to play music.

# play button def buttonPlayClick (): # Select the music folder to play global folder if not folder: folder = tkinter.filedialog.askdirectory () if not folder: return global playing playing = True # create a thread to play music The current main thread is used to receive the user action t = threading.Thread (target=play) t.start () # disable and enable the corresponding button buttonPlay ['state'] =' disabled' buttonStop ['state'] =' normal' buttonPause ['state'] =' normal' buttonNext ['state'] =' normal' pause_resume.set ('Pause') buttonPlay = tkinter.Button (root) as appropriate Text='Play', command=buttonPlayClick) buttonPlay.place (Xerox 20, YB10, width=50, height=20) III. Stop function.

Very direct, directly stop playing, of course, change the state of the button is also a routine operation.

Def buttonStopClick (): global playing playing = False pygame.mixer.music.stop () musicName.set ('no music for the time being') buttonPlay ['state'] =' normal' buttonStop ['state'] =' disabled' buttonPause ['state'] =' disabled' buttonNext ['state'] =' disabled' global folder folder =''buttonStop = tkinter.Button (root, text='Stop') Command=buttonStopClick) buttonStop.place (Xero80, YBE10, width=50, height=20) buttonStop ['state'] =' disabled' IV, pause and resume

This is easy to understand, but also a direct off-the-shelf operation, plus a judgment, state change.

# suspend and resume, the two functions share one button 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') 5. Close

End the ready-made, and then close the form.

# Code executed when closing the program def closeWindow (): # modify the variable, end the loop in the thread global playing playing = False time.sleep (0.3) try: # stop playback, if stopped, # will throw an exception when it stops again So put pygame.mixer.music.stop () pygame.mixer.quit () except: pass root.destroy () 6 in the exception handling structure. Complete code #! / usr/bin/python#-*-coding: UTF-8-*-"" @ author:livingbody@file:music_play.py@time:2022/04/19 "" import osimport tkinterimport tkinter.filedialogimport randomimport timeimport threadingimport pygamefolder =''def play (): # folder is used to represent the folder global folder musics = [folder +'\'+ music for music in os.listdir (folder)\ "where MP3 music files are stored If music.endswith ('.mp3' '.wav' ] # initialize mixer device pygame.mixer.init () while playing: if not pygame.mixer.music.get_busy (): # Random play a song nextMusic = random.choice (musics) musicName.set ('playing....' + nextMusic) pygame.mixer.music.load (nextMusic.encode ( )) # play once pygame.mixer.music.play (1) else: time.sleep (0.3) root = tkinter.Tk () root.title ('music player v1.0') root.geometry ('280x70 / 400 / 300') root.resizable (False False) # Code executed when closing the program def closeWindow (): # modify variables End the loop in the thread global playing playing = False time.sleep (0.3) try: # stop playback, if stopped, # will throw an exception when it stops again So put pygame.mixer.music.stop () pygame.mixer.quit () except: pass root.destroy () root.protocol ('WM_DELETE_WINDOW', closeWindow) pause_resume = tkinter.StringVar (root) in the exception handling structure Value='NotSet') playing = False# play button def buttonPlayClick (): # Select the music folder to play global folder if not folder: folder = tkinter.filedialog.askdirectory () if not folder: return global playing playing = True # create a thread to play music The current main thread is used to receive the user action t = threading.Thread (target=play) t.start () # disable and enable the corresponding button buttonPlay ['state'] =' disabled' buttonStop ['state'] =' normal' buttonPause ['state'] =' normal' buttonNext ['state'] =' normal' pause_resume.set ('Pause') buttonPlay = tkinter.Button (root) as appropriate Text='Play', command=buttonPlayClick) buttonPlay.place (xylene 20, yellow10, width=50 Height=20) # stop button def buttonStopClick (): global playing playing = False pygame.mixer.music.stop () musicName.set ('no music playing temporarily') buttonPlay ['state'] =' normal' buttonStop ['state'] =' disabled' buttonPause ['state'] =' disabled' buttonNext ['state'] =' disabled' global folder folder =''buttonStop = tkinter.Button (root, text='Stop') Command=buttonStopClick) buttonStop.place (Xero80, YBE10, width=50, height=20) buttonStop ['state'] =' disabled'# pause and resume The two functions share a button 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 (Xero140, YFL10, width=50, height=20) buttonPause ['state'] =' disabled'# next music def buttonNextClick (): global playing playing = False pygame.mixer.music.stop () pygame.mixer.quit () buttonPlayClick () buttonNext = tkinter.Button (root, text='Next', command=buttonNextClick) buttonNext.place (Xerox 200 Yidong 10, width=50, height=20) buttonNext ['state'] =' disabled'musicName = tkinter.StringVar (root, value=' is not playing music temporarily...') labelName = tkinter.Label (root, textvariable=musicName) labelName.place (xylene 0, Yanti40, width=270, height=20) if _ _ name__ = ='_ main__': # start message loop root.mainloop () so far I believe you have a deeper understanding of "how to achieve a music player based on Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report