present (sb for a job etc)
This is a simple music player program that uses Python's tkinter library and pygame library. The main features of the program include:
- Add Music Folder
- play music
- Pause/resume playback
- stop playing
- next song
- previous song
- volume control
Ideas for implementation
The program first imports the required libraries and then creates a tkinter window, setting the window title, size and non-stretchability.
Next, several global variables are defined, such as folder path, music list, index of the currently playing music, and so on. Then several functions are defined, such as buttonChooseClick, play, buttonPlayClick, buttonStopClick, buttonNextClick, buttonPrevClick, control_voice, and so on.
The buttonChooseClick function is used to select the music folder to be played and display the music list. play function is used to play the music, which is implemented using the pygame library. buttonPlayClick function is used to start playing the music, buttonStopClick function is used to stop playing the music. buttonNextClick function is used to play the next music, buttonPrevClick function is used to play the previous music. control_voice function is used to control the volume.
Finally, several button click events are defined, and the state of the buttons (e.g. disabled and enabled) is set, as well as the window close event.
In (), the program starts running, displays a window, and waits for user action.
Implementation Code
The first step is to download some music locally
Then you can attach the source code:
# Import import os import time import tkinter import import threading import pygame root = () ('Music Player') ('460x600+500+100') (False,False) # Can't stretch folder ='' res = [] num = 0 now_music = '' num1 = 100 def buttonChooseClick(): """ Add folder :return. """ global folder global res if not folder: folder = () musics = [folder + '\\' + music for music in (folder) \ \ if (('.mp3','.wav','.ogg'))] ret = [] for i in musics: (('\\')[1:]) (('\\','/')) var2 = () (ret) lb = (root,listvariable=var2) (x=50,y=110,width=360,height=400) if not folder: return global playing playing = True # Disable and enable buttons as appropriate buttonPlay['state'] = 'normal' buttonStop['state'] = 'normal' # =buttonPause['state'] = 'normal' pause_resume.set('Play') def play(): """ Play music :return. """ if len(res): () global num while playing: if not .get_busy(): netxMusic = res[num] print(netxMusic) print(num) (()) # Playing (1) if len(res) -1 == num: num = 0 else: num = num + 1 netxMusic = ('\\')[1:] ('playing......' + ''.join(netxMusic)) else: (0.1) def buttonPlayClick(): """ Click to play :return. """ buttonNext['state'] = 'normal' buttonPrev['state'] = 'normal' # Select the music folder to play if pause_resume.get() == 'Play': pause_resume.set('Pause') global folder if not folder: folder = () if not folder: return global playing playing = True # Create a thread to play music, the current main thread is used to receive user actions t = (target=play) () elif pause_resume.get() == 'Pause': # () () pause_resume.set('Continue') elif pause_resume.get() == 'Continue': # () () pause_resume.set('Pause') def buttonStopClick(): """ Stop playback :return. """ global playing playing = False () def buttonNextClick(): """ Next song :return. """ global playing playing = False () global num if len(res) == num: num = 0 playing = True # Create a thread to play the music, and a main thread to receive user actions. t = (target=play) () def closeWindow(): """ Close window :return. """ # Modify the variable to end the loop in the thread global playing playing = False (0.3) try: # Stop playback, if stopped # Throw an exception when stopping again, so put it in the exception structure () () except: pass () def control_voice(value=0.5): """ Sound Control :param value: 0.0-1.0 :return. """ .set_volume(float(value)) def buttonPrevClick(): """ Previous song :return. """ global playing playing = False () # () 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 # Create a thread to play music, the current main thread is used to receive user actions t = (target=play) () # Window closes ('WM_DELETE_WINDOW', closeWindow) # Add button buttonChoose = (root,text='Add',command=buttonChooseClick) # Layout (x=50,y=10,width=50,height=20) # Play button pause_resume = (root,value='Play') buttonPlay = (root,textvariable=pause_resume,command=buttonPlayClick) (x=190,y=10,width=50,height=20) buttonPlay['state'] = 'disabled' # Stop button buttonStop = (root, text='Stop',command=buttonStopClick) (x=120, y=10, width=50, height=20) buttonStop['state'] = 'disabled' # Next song buttonNext = (root, text='Next song',command=buttonNextClick) (x=260, y=10, width=50, height=20) buttonNext['state'] = 'disabled' # Previous song buttonPrev = (root, text='Previous song',command=buttonPrevClick) (x=330, y=10, width=50, height=20) buttonPrev['state'] = 'disabled' # Tags musicName = (root, value='There's no music playing at the moment...') labelName = (root, textvariable=musicName) (x=10, y=30, width=260, height=20) # Volume control # HORIZONTAL for horizontal placement, the default is vertical, vertical for vertical s = (root, label='Volume', from_=0, to=1, orient=, length=240, showvalue=0, tickinterval=2, resolution=0.1,command=control_voice) (x=50, y=50, width=200) # Show ()
rendering (visual representation of how things will turn out)
to this article on the production of simple music player based on Python to this article, more related Python music player content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!