IBepyProgrammer

How to Build a Music Player using Python

4 min read
How to Build a Music Player using Python

In this tutorial, we will learn how to use the Pygame library in Python in conjunction with Tkinter to build a simple music player.

To start, Pygame is an open-source library that makes it easy to develop games, multimedia applications, and graphical user interfaces.

We will use the Pygame library to load, play, and stop an audio file.

To begin we will first use pip to install the Pygame library.

pip install pygame

For this example, we will be using the mixer module from the Pygame library. We will therefore import the mixer module from Pygame.

The mixer module allows us to play/pause, load, and stop audio files in Python.

from pygame import mixer

To use the mixer module we first initialize it using init()

mixer.init()

We then proceed to load a sample file and play the file. Note the audio file is played in the background.

Let's load a sample audio file from our working directory.

mixer.music.load("sample.wav")

Note that we can use the " music.unload()" to unload the sample file to free up resources.

To play the audio file loaded above we run mixer.music.play().

mixer.music.play()

To stop the audio file we can run "mixer.music.stop()".

mixer.music.stop()

Build a Music App with Pygame and Tkinter

To build upon the music player above, we can implement the concepts above to build a simple music app.

Step 1

We first import the required libraries used to build the music player application.

  • Mixer allows us to Play/Pause, Load, and Stop audio files.

  • The line from tkinter import * allows us to import every component in the Tkinter library.

  • The line from tkinter import filedialog allows us to open a dialog box that will allow us to navigate a directory and select files. In this case, we will use this module to select an audio file.

from pygame import mixer
from tkinter import *
from tkinter import filedialog

Step 2

Once we have imported all the libraries required to build the application, we will create a class with methods that will allow us to build the functionality of the application.

  • Let's start by implementing the buttons allowing us to Load, Play, Pause, and Stop music in our window. Note we will be using the "command" method in Tkinter to perform the task of loading, Playing, Pausing, and Stopping the audio track.

  • We can now determine the appropriate positions for the above-created buttons.

  • We then design the method that will allow us to load the audio file into our music player app.

  • We then design the method that will allow us to play the audio file using our music player app.

  • If the condition "audio_file" is met, we will first initialize the mixer. Note that the Mixer allows us to play/pause, load, and stop audio files in Python.

  • We then design the method that will allow us to pause the audio file using our music player app.

  • We then design the method that will allow us to stop the audio file using our music player app.

class music_player:

    def __init__(self, window):

        window.geometry("300x200")

        window.title("Music Player App")

        window.resizable(True, True)

        load_music = Button(window, text="Add Track", width=10, font = ("Helvetica", 15, "bold"), command = self.load_track)

        play_music = Button(window, text="Play Track", width=10, font = ("Helvetica", 15, "bold"), command = self.play_track)

        pause_music = Button(window, text="Pause Track", width=10, font = ("Helvetica", 15, "bold"), command = self.pause_track)

        stop_music = Button(window, text="Stop Track", width=10, font = ("Helvetica", 15, "bold"), command = self.stop_track)

        load_music.place(x=0, y=20)

        play_music.place(x=220, y=20)

        pause_music.place(x=450, y=20)

        stop_music.place(x=220, y=90)

        self.audio_file = False

        self.playing_state = False

    def load_track(self):

        self.audio_file = filedialog.askopenfilename()

    def play_track(self):

        if self.audio_file:

            mixer.init()

            mixer.music.load(self.audio_file)

            mixer.music.play()

    def pause_track(self):

        if not self.playing_state:

            mixer.music.pause()

            self.playing_state = True

        else:

            mixer.music.unpause()

            self.playing_state = False

    def stop_track(self):

        mixer.music.stop()

root = Tk()

player_app = music_player(root)

root.mainloop()

Conclusion

In this article, we learn how to use Pygame and Tkinter to build a simple music player application. In future articles, we will dive deeper into Pygame and Tkinter and how to use these libraries to build more applications.

If you found this article helpful consider subscribing and sharing. A copy of the working code can also be found on my Git hub repository. https://github.com/IBepyProgrammer/Python-Music-App

Thank you.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!