DEV Community

Cover image for Make your own music player in python
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

Make your own music player in python

The original article can be found at kalebujordan.dev

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
Enter fullscreen mode Exit fullscreen mode

  • Window
pip install pygame
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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.

View demo on youtube

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.

GitHub logo Kalebu / MusicPlayer

A music player application made with python for learning purpose

Simple Basic Player implemented in Python using Tkinter & Pygame Library , To read full artilce guiding you step by step on how to build music player please have a visit at kalebujordan.dev

Issues

If you will encounter any issue while trying to run this script, please raise one so as we can fix it as possible

Credits

  1. All the credits to kalebu



Top comments (1)

Collapse
 
sachinp67495829 profile image
Sachin pagar

Thanks for sharing this article but another simple method also available
click on it

how to make your own MP3 music player in python using Tkinter