Make your own Music Player in Python

Make your own Music Player in Python

The original article can be found at kalebujordan.com

Make your own python music player

In this tutorial, you're going to learn how to make your own MP3 Music Player in Python using pygame and Tkinter libraries which is able to load, play, pause, and stop the music.

Requirements

To be able to follow this tutorial you need to have Tkinter and pygame installed on your machine

Installation

  • Linux
sudo apt-get install python3-tk

pip3 install pygame

  • Window
    pip install pygame
    

Now once everything is ready Installed, we now ready to begin coding our very own music player

Let's get started

We gonna use Tkinter to render our application menu and buttons, and also loading the music from files and then pygame library to play, pause and stop the music

Basics of pygame

Pygame has an inbuilt method called mixer () which provides us intuitive syntax on dealing with sounds files in python, we will see ;

  • loading and playing music
  • pausing and unpausing music
  • stoping the music file

loading and playing music with pygame

To play music with pygame you're firstly supposed to import mixer(), initialize it through init(), and then using music.load() to load the music and finally playing it with music.play().

Example of usage

from pygame import mixer

mixer.init() #Initialzing pyamge mixer

mixer.music.load.('filename.mp3') #Loading Music File

mixer.music.play() #Playing Music with Pygame

Pausing and unpausing music with pygame.

use music.pause() and music.unpause() to pause and unpause your music, but make your music file is loaded first before using these methods.

Example of usage

mixer.music.pause() #pausing music file

mixer.music.unpause() #unpausing music file

Stop a music file

use music.stop() to stop your music completely from playing, once you use this method the loaded music will be cleared in pygame memory.

mixer.music.stop()

Building your music player exoskeleton

We have already covered the basics of playing music with python, now it's time to begin designing our application user interface{UI) and then link together with the logics.

First Let us import all necessary Library

from tkinter import *

from tkinter import filedialog

from pygame import mixer

Let's now implement our class & Buttons for our application

class MusicPlayer:
    def __init__(self, window ):
        window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60)
        self.music_file = False
        self.playing_state = False

Let's now add the method to the class we just made to load music file from our computer, just as shown in the code below

Adding Load Method to our MusicPlayer class

class MusicPlayer:
    def __init__(self, window ):
        window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60)
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()

After Loading the Music file from the file we need a function to Play our Music File, Let's make it using the concepts we just learned above.

Adding Play Method to our class

class MusicPlayer:
    def __init__(self, window ):
        window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60)
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()
    def play(self):
        if self.music_file:
            mixer.init()
            mixer.music.load(self.music_file)
            mixer.music.play()

After adding the Play Method to our class we need a Method to pause and unpause & also to Stop the Music

Finally Let's add the pause and stop method to our class

class MusicPlayer:
    def __init__(self, window ):
        window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60)
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()
    def play(self):
        if self.music_file:
            mixer.init()
            mixer.music.load(self.music_file)
            mixer.music.play()
    def pause(self):
        if not self.playing_state:
            mixer.music.pause()
            self.playing_state=True
        else:
            mixer.music.unpause()
            self.playing_state = False
    def stop(self):
        mixer.music.stop()

Let's look at our Final will app (app.py)

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

class MusicPlayer:
    def __init__(self, window ):
        window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
        Load = Button(window, text = 'Load',  width = 10, font = ('Times', 10), command = self.load)
        Play = Button(window, text = 'Play',  width = 10,font = ('Times', 10), command = self.play)
        Pause = Button(window,text = 'Pause',  width = 10, font = ('Times', 10), command = self.pause)
        Stop = Button(window ,text = 'Stop',  width = 10, font = ('Times', 10), command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60) 
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()
    def play(self):
        if self.music_file:
            mixer.init()
            mixer.music.load(self.music_file)
            mixer.music.play()
    def pause(self):
        if not self.playing_state:
            mixer.music.pause()
            self.playing_state=True
        else:
            mixer.music.unpause()
            self.playing_state = False
    def stop(self):
        mixer.music.stop()
root = Tk()
app= MusicPlayer(root)
root.mainloop()

Output

If you run the above code the output will be as shown below, now you can play around with it by loading, playing, pausing, and stopping the music, full demo in youtube link at the end of the article.

Based on your interest I recommend you to also check these;

In case of any comment,suggestion or difficulties drop it on the comment box below, and then I will get back to you ASAP.

To get the full code for this article, here a link to My GitHub