In this article, I am going to create a music-playing interface for windows 10’s users with python with the help of pygame framework! Pygame has a mixer module that can play music and it is basically good enough for us to create a functional music player in windows 10, the only problem is there is some music format which it will not play, for example, the wav audio file, but it is alright since most of the music files at this moment is actually OGG and MP3.
My plan here is simple, create the interface with the Tkinter and then used pygame mixer as the music playing engine…
Here is the entire code…
# This is a music player project created with Pygame Framework import tkinter as tk from tkinter import ttk from tkinter import filedialog import pygame # create the windows for this music player win = tk.Tk() win.title("Easy Play") win.resizable(0,0) # the music file link is initially empty music_file = "" # 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] pygame.mixer.init() file_opener= ttk.Button(win, text="Open File", command=openFile) file_opener.grid(column=0,row=0) # play the music file def playMusic(): if(music_file!=''): pygame.mixer.music.load(music_file) pygame.mixer.music.play() play_button = ttk.Button(win, text="Play", command=playMusic) play_button.grid(column=1,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(win, text="Pause", command=pauseMusic) pause_button.grid(column=2,row=0) # unload the music resource from the memory def unloadMusic(): pygame.mixer.music.unload() unload_button = ttk.Button(win, text="Unload", command=unloadMusic) unload_button.grid(column=3,row=0) def stopMusic(): pygame.mixer.music.stop() stop_button = ttk.Button(win, text="Stop", command=unloadMusic) stop_button.grid(column=4, row=0) win.mainloop()
As the comments on the above program have already self-explained every function above I will just roughly explain to you what this program does.
1) When the user clicks on the open file button the file dialog box will appear where the user can then picks a music file he or she wants to hear about.
2) The file is basically ready but not played yet until the user clicks the play button.
3) The user can also click on the stop button to stop the music altogether or clicks on the pause button to pause the song and then clicks again on the pause button to continue playing the music. There is another unload button that is used to release the music on the computer memory.
If you select nothing and press the play button the music will not play! Below is the interface and I am actually planning to put more stuff to the below simple interface to make it a completely functional music player!
