Hello again, after a few days of working on my other projects, I have some time to continue with my previous music player project and again in this article, I will publish what I have achieved so far. I would always want to be as details as possible in my program explanation but sometimes no matter how much I have put my afford into it you might still don’t understand the entire program, oh well, just make sure you read the comment on the program and hope you will eventually understand the entire program.
What are the latest features in this new version of the music player?
1. Instead of letting a user opens a file on his own this program will let the user select a folder and then load all the mp3 and Ogg files into the combo box where the user can select the song to play and then clicks on the play button to play the song!
2. The volume slider feature which allows you to increase and decrease the song volume, remember to increase the volume first after you have pressed the play button because as you can see the volume always stays at 0 at the beginning which means you will need to increase the volume or else it will stay at 0 and you will hear no sound at all!
3. Also as compared to the previous version the panel has become larger in size because more features will come later!
And yes that is basically just a small feature one at a time and I will write it when I am free and not busy!
Below is the entire program, remember you will need to load the files first and then select one of them in order to play the music.
# This is a music player project created with Pygame Framework import glob import os import re from tkinter import ttk, W, E, Tk, Frame, StringVar, DoubleVar, Scale, Label from tkinter import filedialog import pygame from tkinter import scrolledtext # create the windows for this music player import tk win = Tk() win.geometry("600x300") win.title("Easy Play") win.resizable(0,0) #tuple and dictionary of song playlist play_list_list = list() play_list_dict = dict() # the music file link is initially empty music_file = "" # create the main music player frame tab mainControl = ttk.Notebook(win) mainControlTab = ttk.Frame(mainControl) mainControl.add(mainControlTab, text="Player") mainControl.pack(expand=1, fill="both") #create a button frame which holds all the buttons button_frame = Frame(mainControlTab, bg='black') button_frame.grid(column=0, row=0, sticky=E+W) #create a play list frame which holds all the buttons play_list_frame = Frame(mainControlTab) play_list_frame.grid(column=1, row=0, sticky=E+W) #initialize the music mixer module pygame.mixer.init() # open music file and initialize the mixer #def openFile(): #global music_file #fullfilenames = filedialog.askopenfilenames(initialdir="/", title="Select music file", filetypes=[("OGG format", ".ogg"), #("MP3 format", ".mp3"),]) # select a music file #if (fullfilenames != ''): #music_file = fullfilenames[0] #file_opener= ttk.Button(button_frame, text="Open File", command=openFile) #file_opener.grid(column=0,row=0) # play the music file def playMusic(): music_file = play_list_dict[track.get()] if(music_file!=''): pygame.mixer.music.load(music_file) volumn = volumn_slider.get()/100 pygame.mixer.music.set_volume((volumn)) pygame.mixer.music.play() play_button = ttk.Button(button_frame, text="Play", command=playMusic) play_button.grid(column=0,row=0) # pause the music file pause = False def pauseMusic(): global pause if(pause == False): pygame.mixer.music.pause() pause = True else: pygame.mixer.music.unpause() pause = False pause_button = ttk.Button(button_frame, text="Pause", command=pauseMusic) pause_button.grid(column=1,row=0) # unload the music resource from the memory def unloadMusic(): pygame.mixer.music.unload() unload_button = ttk.Button(button_frame, text="Unload", command=unloadMusic) unload_button.grid(column=2,row=0) def stopMusic(): pygame.mixer.music.stop() stop_button = ttk.Button(button_frame, text="Stop", command=unloadMusic) stop_button.grid(column=3, row=0) # open music files and shows them on the list box def showFile(fulllist): for path in os.scandir(fulllist): if path.is_file(): current_abs_url = (os.path.abspath(path)) pattern = "^([a-z0-9])+\.ogg$" pattern1 = "^([a-z0-9])+\.mp3$" g = re.search(pattern, path.name) f = re.search(pattern1, path.name) if g != None : print(g.group(0)) play_list_list.insert(0,g.group(0)) play_list_dict[g.group(0)] = current_abs_url elif f != None: play_list_list.insert(0,f.group(0)) play_list_dict[f.group(0)] = current_abs_url else: showFile(path) track_music['values'] = play_list_list def loadList(): global music_file fulllist = filedialog.askdirectory() # select a music directory if (fulllist != ''): showFile(fulllist) directory_opener= ttk.Button(play_list_frame, text="Open", command=loadList) directory_opener.grid(column=0,row=0) # create the scrolltext field to inout the music player list #scrolW = 100 #scrolH = 100 #playerList = scrolledtext.ScrolledText(play_list_frame, width=scrolW, height=scrolH,wrap=tk.WORD) #playerList.grid(column=0, row=0, columnspan=3) #combo box of music tracks track = StringVar() track_music = ttk.Combobox(play_list_frame, width=20, textvariable=track) track_music.grid(column=1, row=0) #track_music.current(0) def volumn_slider_adjust(val): volumn = volumn_current_value.get()/100 pygame.mixer.music.set_volume(volumn) #create music volumn slider volumn_current_value = DoubleVar() volumn_slider = Scale( play_list_frame, from_=0, to=100, orient='horizontal', variable=volumn_current_value, command=volumn_slider_adjust ) volumn_slider.grid(column=0, row=1) volumn_label = Label(play_list_frame, text = "Volumn") volumn_label.grid(column=1, row=1) win.mainloop()
Basically, that is it, don’t forget to subscribe to this blog to get the latest information on my homemade program which is free to use on your windows OS or another OS if it is possible to do so!