MP3 Music Player in Python
MP3 Music Player in Python
Lincoln May
Table of Contents
1
1. Introduction and Goals
2. Code Explanation
3. Challenges Faced 6
4. What I learned
14
2
Introduction and Goal:
When first reading about the final project for CSE 120, I was extremely
excited with the broadness of the project. It was great to have the ability to
choose to do the project on your own personal interests, which has been
uncommon in my university experience thus far. That being said, there was
that I had was to recreate a music app similar to Spotify or Apple Music. After
doing a little research on this idea, I saw that you could use pygame, a
library used for developing games in python, to play mp3 files. While
mp3 music player application using pygame and Tkinter. I wanted users to
be able to import their own folder of mp3 files and be able to play, pause,
and navigate through their music collection. I wanted the user interface to be
simple and intuitive. It includes buttons to play, pause, next and previous
3
see what songs are next up and a volume slider to allow the users to easily
Code Explanation:
The first few lines of code import the necessary libraries for the project.
Pygame is used for the music playback functionalities, while Tkinter is used
to provide tools for creating the graphical user interface. The filedialog
module is used to open a dialog for directory selection, and OS handles file
The next segment of code initializes pygame mixer which is used for
create the main window for my application and set the title to “Music
Player”. I then set the dimension of the window and add a menu bar, which
After most of the setup has been taken care of, I create a load song
function. This Function allows the user to select a directory containing mp3
then scans the selected directory for MP3 files, adds them to the songs list,
and populates the song_list window with these songs. The first song in the
4
The play song function manages the playback of the selected song. If
the song is not paused, it loads and plays the currently selected song from
The pause song function pauses the current song playback. It uses
The next song function moves to the next song in the list and starts
playing it. It calculates the index of the next song, updates the curr_song
variable, and loads and plays the new song using the play song function. It
also updates the selection in the song_list window to reflect the currently
playing song.
The previous song function moves to the previous song in the list and
starts playing it. It also calculates the index of the previous song, updates
the curr_song variable, and loads and plays the new song using the play
song function, updating the selection in the song_list window to reflect the
The set volume function adjusts the playback volume. It takes a value
from the volume slider, converts it to a range between 0 and 1, and sets the
5
The next segment sets up the menu and the list box in the main
window. The menu allows the user to select a folder containing MP3 files
using the load_song function. The song_list List box displays the list of songs
The final segment creates and positions the control buttons and the
volume slider. The buttons for previous, play, pause, and next functionalities
are created using tk.Button and they are open-source PNG images from the
The volume slider is created using tk.Scale, oriented horizontally, and linked
to the set_volume function. The default volume is set to 70%. Then I call
Challenges Faced:
Overall, this project was very fun to develop but there was some
challenges faced along the way. One of the main challenges I faced initially
was that both Tkinter and pygame were completely new to me. I expected to
come across new things while working on this project but it was still
challenging learning how to use pygame and Tkinter. Luckily pygame was
not too complicated, but tkinter was a bit confusing. I have some experience
working with JavaFX, which I found similar to Tkinter. But I seem to have a
hard time understanding both. After a little research and practice I was able
6
Another challenge I faced while developing this project was using
filedialog and os modules. One significant challenge was ensuring that files
challenge to only append mp3 files to the song list, avoiding potential errors
interaction, this is because the user has to be aware that only mp3 files are
supported and it will result in errors if the user tries importing a different
reflect when the previous or next song buttons were selected was also
difficult to work out. I had to make sure that the program would not
only did they strengthen my understanding of python, but they also worked
What I Learned:
pygame for music playback was a significant aspect of this project. These
7
provides the tools to build a user-friendly interface, while pygame handles
ensuring that the user interface responds correctly to audio events and user
inputs. Understanding this integration was crucial for the project's success
Tkinter relies heavily on events and callbacks to interact with the user, such
that user inputs are processed immediately and correctly. Working with
and managing file paths, was another important learning outcome of this
project. Using the os module, I was able to navigate directories, filter files by
their extensions, and manage file paths efficiently. This skill is crucial for any
application that interacts with the file system, and it ensured that the music
player could dynamically load and manage songs from different directories
8
.
Main.py:
import pygame
import tkinter as tk
import os
pygame.mixer.init()
9
root = tk.Tk()
root.title("Music Player")
root.geometry("500x400")
menubar = tk.Menu(root)
root.config(menu=menubar)
def load_song():
global curr_song
root.directory = filedialog.askdirectory()
if ext == '.mp3':
songs.append(song)
song_list.insert("end", song)
if songs:
song_list.selection_set(0)
curr_song = songs[song_list.curselection()[0]]
def play_song():
10
if not paused:
pygame.mixer.music.load(os.path.join(root.directory, curr_song))
pygame.mixer.music.play()
else:
pygame.mixer.music.unpause()
paused = False
def pause_song():
global paused
pygame.mixer.music.pause()
paused = True
def next_song():
try:
current_index = songs.index(curr_song)
curr_song = songs[next_index]
song_list.selection_clear(0, END)
song_list.selection_set(next_index)
song_list.activate(next_index)
pygame.mixer.music.load(os.path.join(root.directory, curr_song))
pygame.mixer.music.play()
11
paused = False
except IndexError:
pass
def prev_song():
try:
current_index = songs.index(curr_song)
curr_song = songs[prev_index]
song_list.selection_clear(0, END)
song_list.selection_set(prev_index)
song_list.activate(prev_index)
pygame.mixer.music.load(os.path.join(root.directory, curr_song))
pygame.mixer.music.play()
paused = False
except IndexError:
pass
def set_volume(val):
pygame.mixer.music.set_volume(volume)
12
organize_menu = tk.Menu(menubar, tearoff=False)
song_list.pack()
songs = []
curr_song = ""
paused = False
play_img = tk.PhotoImage(file="play.png")
pause_img = tk.PhotoImage(file="pause.png")
prev_img = tk.PhotoImage(file="previous.png")
next_img = tk.PhotoImage(file="next.png")
control_frame.pack()
13
prev_button.grid(row=0, column=0, padx=10, pady=10)
volume_slider.set(70)
volume_slider.pack(pady=10)
root.mainloop()
14
In the third week of the project, I got familiar with pygame
and tkinter before I was to start building my project. I
Week 3, 6/16 messed around with tkinter in the ide to get more familiar
with the syntax and usage and did the same with pygame
to a lesser extent.
15