Kalebu Jordan

Become a Pro Python Developer

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

# In Linux
sudo apt-get install python3-tk 
pip3 install pygame

# In 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().

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.

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 method to class we just made to load music file from file , add below line to class

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 function to Play our Music File, Let’s make it using the knowledge we learnt at very first

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 Play Method to our class we need a Method to pause and unpause & also to stop the Music

Finally Let’s add 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;

Don’t forget to subscribe to this blog to stay updated on upcoming python tutorials, the complete source code of the music player above can be found on My GITHUB

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

3 thoughts on “Make your own Music Player in Python

Leave a Reply

error

Enjoy this blog? Please spread the word :)

%d bloggers like this: