Tutorial: Program a simple music player in Python

In this tutorial we are going to program a music player for Windows in the Python language. We will use two base libraries for this: PySimpleGUI for the development of the graphical interface and Pygame for the audio reproduction.

First we import the necessary libraries:

If you don't have pygame, you can install it using pip install pygame, for pysimplegui you can do it the same way, although it will not be necessary since in the code files I will include a file that contains pysimplegui.

Now, let's develop the player, first we need to define a player class and inside it, as a first point develop the graphical interface.

We will define our interface with 2 top buttons, one to select an individual song and one to select an entire folder, we will also add a central image and 3 buttons to pause the song, go back and continue to the next.

We will also add several variables that will allow us to control the operation of the player.

The main function will be to play music, in this case using the mixer module of the pygame library it will be easy to implement, in this way:

In our role, we will receive an absolute path about the song to be played, initializing the player with predefined parameters and creating the controls for pause and continue. The state variable will let us know that a song is currently playing.


Now, let's add a couple of functions to our class, that will allow us to select a song or a complete folder:

In this case we are going to use threads, which will allow us to continue playing music even if we are performing other tasks and thus prevent our program from crashing. Both functions are similar, the difference is that one of them only allows you to play a song, and the other a list of several songs. Both ending by calling our function reproduce before created.

Now we are going to define the main operation of the player, that will allow you to run the graphic interface and control the song selection and playback processes, the code is the following:

The while loop will always be executed, unless you exit the program, which will allow updating the graphical interface and receiving events from it.

Source code

If the click event is received, the option to load a song was selected, either through a folder or individual, we will run a popup to allow the user to select the file.

Once the folder or file is selected, we will execute the function to load song, using the threads, and thus starting the song playback.

If a folder is selected, we will have a list of songs to play, with which we can advance them, to return, pause and play. Controls run like this:

So we can fully control its execution. If we want to exit the program, we just have to click on the close icon.

The final operation, it will be as follows:

To end, I must clarify that although we can play music easily using pygame and mp3 files are supported, under certain encodings it is impossible to play some mp3 files causing errors, where the ideal is to play only .ogg files, although we can perform certain tricks by converting .mp3 to .ogg at runtime using tempfiles in Python and thus make the player completely compatible.

As always, the source code you can find below:

Source code

1 thought on “Tutorial: Program a simple music player in Python”

  1. Hello! I'm starting with Python and your publication has helped me a lot. I would like to be able to use the player in a web application but I can't get it to work. In the console I get many errors that I cannot solve. Can you tell me if this player can be used on a web? Or that I would have to change? Thanks in advance, a greeting!

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 5 =