Music Player Application Python Project

Everyone enjoys listening to music, therefore wouldn't it be great to have our own MP3 player? So, in this python project, we're going to use python and its libraries to make an mp3 player. Let's get started on the mp3 player in Python project.
MP3 Music Player Application in Python Language
We'll make an mp3 music player that allows us to play, stop, and restart tracks, as well as move from one song to the next and previous ones.
Python and associated libraries will be used. The first package we'll use is Tkinter, a widely used GUI toolkit provided by Python. We don't need to install it separately since it's included with Python.
The mixer module of Pygame, a well-known Python library, will be used next.
Pygame is a programming language that is used to develop video games and contains graphics and sound libraries. One such sound library is Mixer. Then, to interface with the operating system, we'll utilize Python's os package.
Prerequisites for the Project
Working on a python mp3 player requires a basic familiarity of the Python programming language, tkinter, and the Pygame mixer module.
A basic grasp of Tkinter widgets is also necessary, but don't worry, we'll go through every line of code as we build this mp3 player project.
Unlike the Tkinter library, the Pygame library must be installed.
To install pygame, use the following line in your command prompt or terminal.
pip install pygame
Steps to take in order to complete this project
- Import essential libraries
- Make a project layout.
- Define the functionality of the music player, such as play, pause, and other options.
Source Code for MP3 Music Player Application
#importing libraries from pygame import mixer from tkinter import * import tkinter.font as font from tkinter import filedialog #creating the root window root=Tk() root.title('DataFlair Python MP3 Music player App ') #initialize mixer mixer.init() #create the listbox to contain songs songs_list=Listbox(root,selectmode=SINGLE,bg="black",fg="white",font=('arial',15),height=12,width=47,selectbackground="gray",selectforeground="black") songs_list.grid(columnspan=9) #font is defined which is to be used for the button font defined_font = font.Font(family='Helvetica') #play button play_button=Button(root,text="Play",width =7,command=Play) play_button['font']=defined_font play_button.grid(row=1,column=0) #pause button pause_button=Button(root,text="Pause",width =7,command=Pause) pause_button['font']=defined_font pause_button.grid(row=1,column=1) #stop button stop_button=Button(root,text="Stop",width =7,command=Stop) stop_button['font']=defined_font stop_button.grid(row=1,column=2) #resume button Resume_button=Button(root,text="Resume",width =7,command=Resume) Resume_button['font']=defined_font Resume_button.grid(row=1,column=3) #previous button previous_button=Button(root,text="Prev",width =7,command=Previous) previous_button['font']=defined_font previous_button.grid(row=1,column=4) #nextbutton next_button=Button(root,text="Next",width =7,command=Next) next_button['font']=defined_font next_button.grid(row=1,column=5) #menu my_menu=Menu(root) root.config(menu=my_menu) add_song_menu=Menu(my_menu) my_menu.add_cascade(label="Menu",menu=add_song_menu) add_song_menu.add_command(label="Add songs",command=addsongs) add_song_menu.add_command(label="Delete song",command=deletesong) mainloop() #add many songs to the playlist of python mp3 player def addsongs(): #to open a file temp_song=filedialog.askopenfilenames(initialdir="Music/",title="Choose a song", filetypes=(("mp3 Files","*.mp3"),)) ##loop through every item in the list to insert in the listbox for s in temp_song: s=s.replace("C:/Users/DataFlair/python-mp3-music-player/","") songs_list.insert(END,s) def deletesong(): curr_song=songs_list.curselection() songs_list.delete(curr_song[0]) def Play(): song=songs_list.get(ACTIVE) song=f'C:/Users/lenovo/Desktop/DataFlair/Notepad/Music/{song}' mixer.music.load(song) mixer.music.play() #to pause the song def Pause(): mixer.music.pause() #to stop the song def Stop(): mixer.music.stop() songs_list.selection_clear(ACTIVE) #to resume the song def Resume(): mixer.music.unpause() #Function to navigate from the current song def Previous(): #to get the selected song index previous_one=songs_list.curselection() #to get the previous song index previous_one=previous_one[0]-1 #to get the previous song temp2=songs_list.get(previous_one) temp2=f'C:/Users/DataFlair/python-mp3-music-player/{temp2}' mixer.music.load(temp2) mixer.music.play() songs_list.selection_clear(0,END) #activate new song songs_list.activate(previous_one) #set the next song songs_list.selection_set(previous_one) def Next(): #to get the selected song index next_one=songs_list.curselection() #to get the next song index next_one=next_one[0]+1 #to get the next song temp=songs_list.get(next_one) temp=f'C:/Users/DataFlair/python-mp3-music-player/{temp}' mixer.music.load(temp) mixer.music.play() songs_list.selection_clear(0,END) #activate newsong songs_list.activate(next_one) #set the next song songs_list.selection_set(next_one)
Output

Summary
Congratulations! We have successfully developed a python mp3 music player, and we no longer need to depend on any other application.
We learnt a lot about python and its libraries via this project, the first of which was the Tkinter library, which is a widely-used GUI library with a variety of widgets, and then the essential mixer module of the pygame library, which is used to alter the music.