How to build an MP3 Music Player with Python

Understanding the infinite possibilities of Python



3 years ago | 4 min read


In this article, I will show you how you can create your own music player using python.

Yes, I know I am a data analyst, but sometimes when I work with data I like listening to music. Data Analysis with music can be a very interesting mix of inspiration and concentration for a data analyst or a data scientist.

And building your own music player is an amazing way to learn more about python and its potential. Maybe some modules that I will use in this article could be helpful in the deployment of your next machine learning model.

If you follow each step in this article, the final output will be like this!

So, let’s get started!

Part I: Set the environment and import modules

The first step is to create a directory called environments where to store all your virtual Environments and install the python modules. In this way the program is available only within the set environment and not throughout my computer.

So, open your terminal and create a directory as below.

mkdir Environments

And be sure to be into the directory ‘Environments’.

cd C:\Users\moryb\Environments

The second step is to use a module virtualenv to create isolated virtual environments.

pip install virtualenv

Now, we can create a virtual environment named myenv. If you have installed python 3 you can easily digit:

virtualenv myenv

Inside the directory myenv there is a copy of Python interpreter, the standard library and the various supporting files.

We can activate the environment with the following command:



(myenv) C:\Users\moryb\Environments>

Then, we need to install Pygame. Pygame is a module that is used for creating videogames. For creating our MP3 Music Player we will use the sound component.

pip install pygame

In this second phase, I will use Atom as python editor.

So I will import the following modules.

import pygame #used to create video games
import tkinter as tkr #used to develop GUI
from tkinter.filedialog import askdirectory #it permit to select dir
import os #it permits to interact with the operating system

Part II: Create the window of your music player and write the prompt command for music selection

After we have imported the modules, we need to create the interface and establish the interaction with our operating system.

So to create the interface, we will write the following code that permits to create the interface, add the title of the interface and set the dimension of the interface.

music_player = tkr.Tk()
music_player.title(“Life In Music”)

Now, it is time to create a directory that prompts the user to choose the folder where the music files are listed. To do that the OS module will help us to create interaction between the interface and our operating system.

directory = askdirectory()
os.chdir(directory) #it permits to chenge the current dir
song_list = os.listdir() #it returns the list of files song

Instead, the following code presents a variable called play_list which brings the interface to display the items to the user. In addition, we create a for loop in order to push the program to select each item from the song_list and insert them into the Listbox.

play_list = tkr.Listbox(music_player, font=”Helvetica 12 bold”, bg=”yellow”, selectmode=tkr.SINGLE)for item in song_list:
pos = 0
play_list.insert(pos, item)
pos += 1

For what concerns loading and playing sounds, we use pygame as below.


Part III: Writing functions to control the music player

In this last part, we will see how to create commands such as play, stop, pause and unpause in order to control the music player. To create these commands we will build some functions and we will use tkinter to create the button in the interface.

def play():
var.set(play_list.get(tkr.ACTIVE)) stop(): pause(): unpause():

Then we can create the buttons for the interface of our music player.

Button1 = tkr.Button(music_player, width=5, height=3, font=”Helvetica 12 bold”, text=”PLAY”, command=play, bg=”blue”, fg=”white”)
Button2 = tkr.Button(music_player, width=5, height=3, font=”Helvetica 12 bold”, text=”STOP”, command=stop, bg=”red”, fg=”white”)
Button3 = tkr.Button(music_player, width=5, height=3, font=”Helvetica 12 bold”, text=”PAUSE”, command=pause, bg=”purple”, fg=”white”)
Button4 = tkr.Button(music_player, width=5, height=3, font=”Helvetica 12 bold”, text=”UNPAUSE”, command=unpause, bg=”orange”, fg=”white”)

Now, we create some variables that permit us to see the song that is running on the top of the application.

var = tkr.StringVar()
song_title = tkr.Label(music_player, font=”Helvetica 12 bold”, textvariable=var)

With the pack method, we can arrange our button in a horizontal way. The last code music_player.mainloop() helps us to run the application.

play_list.pack(fill=”both”, expand=”yes”)music_player.mainloop()

Finally, to run the music player, go to your terminal, activate the environment and run the program that we have saved as MyPlayer.

C:\Users\moryb\Environments>myenv\Scripts\activate.bat(myenv) C:\Users\moryb\Environments>


Now, you have your own music player and you can listen to songs when you work with data. Sometimes working with data could be very challenging and having your own mp3 music player is very helpful.

In addition, I think that building an mp3 music player or other applications can really help you to know new Python modules and at the same time learn more about Python programming.

I always say that the better way to improve your programming skills is building something or automating a process. That permits you to have more confidence with programming and it is very useful for your personal portfolio. All these aspects can become your points of strength as a Data Analyst or Data Scientist.


Created by


Data Analyst and Data Lover







Related Articles