Learn How to Create a Music Player in Python

Proxlight
2 min readNov 28, 2020

--

Work on an interesting Python Project — Music Player and boost your confidence.

Music washes away the dust of everyday life from the soul
-Berthold Auerbach.

The saying was indeed true, we all love music. Also, we don’t prefer any kind of disturbance or ads in between but this seems impossible without a paid subscription, so let’s try to design a music player, just like the way we want.

Let’s discuss it in technical terms.

Project Prerequisites

The prerequisites are as follows :

  1. Basic Python concepts
  2. Tkinter

To install the libraries, you can use pip installer from the cmd/Terminal:

Pip install tkinter

Download Python Music Player Code

Created By Pratyush Mishra

Please download the code of python music player project: Music Player Source Code

Let’s start the coding

Video Tutorial

Now, we will write the python program to create a music player

Create main.py

Create main.py file and add the following code (alternatively, you can use the code which you downloaded in previous step):

Code:

import pygame

from pygame import mixer

from tkinter import *

import os

def playsong():

currentsong=playlist.get(ACTIVE)

print(currentsong)

mixer.music.load(currentsong)

songstatus.set(“Playing”)

mixer.music.play()

def pausesong():

songstatus.set(“Paused”)

mixer.music.pause()

def stopsong():

songstatus.set(“Stopped”)

mixer.music.stop()

def resumesong():

songstatus.set(“Resuming”)

mixer.music.unpause()

root=Tk()

root.title(‘Music player project’)

mixer.init()

songstatus=StringVar()

songstatus.set(“choosing”)

#playlist — — — — — — — -

playlist=Listbox(root,selectmode=SINGLE,bg=”DodgerBlue2",fg=”white”,font=(‘arial’,15),width=40)

playlist.grid(columnspan=5)

os.chdir(r’C:\Users\BOSS\Desktop\MyPlaylist’)

songs=os.listdir()

for s in songs:

playlist.insert(END,s)

playbtn=Button(root,text=”play”,command=playsong)

playbtn.config(font=(‘arial’,20),bg=”DodgerBlue2",fg=”white”,padx=7,pady=7)

playbtn.grid(row=1,column=0)

pausebtn=Button(root,text=”Pause”,command=pausesong)

pausebtn.config(font=(‘arial’,20),bg=”DodgerBlue2",fg=”white”,padx=7,pady=7)

pausebtn.grid(row=1,column=1)

stopbtn=Button(root,text=”Stop”,command=stopsong)

stopbtn.config(font=(‘arial’,20),bg=”DodgerBlue2",fg=”white”,padx=7,pady=7)

stopbtn.grid(row=1,column=2)

Resumebtn=Button(root,text=”Resume”,command=resumesong)

Resumebtn.config(font=(‘arial’,20),bg=”DodgerBlue2",fg=”white”,padx=7,pady=7)

Resumebtn.grid(row=1,column=3)

mainloop()

Libraries Used

  1. Pygame: to play, pause, load, stop, and resume music.
  2. Tkinter: to develop GUI.
  3. os: to access the song folder.

Functions Used

  1. playsong: It loads the active song from the list and plays the required song. It gets executed when the user clicks on “play”.
  2. pausesong: It pauses the required song. It gets executed when the user clicks on “pause”.
  3. stopsong: It stops the required song. It gets executed when the user clicks on “stop”.
  4. resumesong: It resumes the required song. It gets executed when the user clicks on “resume”.

Variables Used

  1. root: the main GUI window.
  2. song status: it stores the status of the currently active song.
  3. playlist: It stores the name of all songs available at the specified location.

Rest are play, pause, stop, and resume buttons.

Python Music Player Output

Summary

We have successfully developed a music player in python. In this project, we have used Tkinter & Pygame APIs of python.

--

--

Proxlight

Welcome to Proxlight, a place for all to learn coding and fun stuff.