Learn How to Create a Music Player in Python

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

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

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

Let’s start the coding

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('ProjectGurukul 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. songstatus: 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

python music player

Summary

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

Did you like this article? If Yes, please give ProjectGurukul 5 Stars on Google | Facebook

9 Responses

  1. Abdelrahman Khaled Abdulmuniem Younis says:

    These is a very good Project .

  2. Sanika says:

    Very good

  3. dave says:

    first change the selectmode to MULTIPLE
    then use for loop inside play_song function

  4. dave says:

    well you should choose your songs from your OWN directory from your folder .
    it will work

  5. karthik says:

    hey can this music player play songs that are not stored in the computer?

  6. alex says:

    how can we play next song? any ideas?

  7. rocky says:

    how we can loop these without playing each song all time.

  8. chandrakant says:

    HI ,,,im getting error in line 37 “no file such directory”

    • ravenloue says:

      Obviously you need to adjust the directory to your personal music folder. Not everyone is gonna have a user named “BOSS”

Leave a Reply

Your email address will not be published. Required fields are marked *