DEV Community

Cover image for Make Your Own Music Player Using Python 🎧
Mr. Unity Buddy
Mr. Unity Buddy

Posted on • Updated on • Originally published at mr-unity-buddy.hashnode.dev

Make Your Own Music Player Using Python 🎧

Hello, buddies! So, Today, we are going to make our own music player with Python with GUI. No more talks, get ready with your interpreter!

Wait, why do I want it? Well, my music player doesn't work and I want a new one πŸ˜‘

image.png

Prerequisites

  • Knowledge about Python Basics
  • Install Tkinter and PyGame. Then, we have to import modules.
import pygame
from pygame import mixer
from tkinter import *
import os
Enter fullscreen mode Exit fullscreen mode

Coding Time

Your most loving part. Let's start!

def playsong():
    currentsong=playlist.get(ACTIVE)
    print(currentsong)
    mixer.music.load(currentsong)
    songstatus.set("Playing")
    mixer.music.play()
Enter fullscreen mode Exit fullscreen mode
  • playsong() function is used to play the music. It loads the active song from the list and plays the required song. It gets executed when the user clicks on β€œplay”.
    • currentsong function gets the active song/music in the PlayList and print it. In song status, it sets 'Playing'.
def pausesong():
    songstatus.set("Paused")
    mixer.music.pause()

def stopsong():
    songstatus.set("Stopped")
    mixer.music.stop()

def resumesong():
    songstatus.set("Resuming")
    mixer.music.unpause()
Enter fullscreen mode Exit fullscreen mode
  • All these 3 functions are related to each other. -pausesong() pause the song and set status to "Paused". -stopsong() and resumesong() do the same as their names.
root=Tk()
root.title('Buddy Music player')

mixer.init()
songstatus=StringVar()
songstatus.set("choosing")
Enter fullscreen mode Exit fullscreen mode
  • In here, root is the main GUI window. root.title set a title to the window. (Change it as you want;)
playlist=Listbox(root,selectmode=SINGLE,bg="DodgerBlue2",fg="white",font=('arial',15),width=40)
playlist.grid(columnspan=5)
Enter fullscreen mode Exit fullscreen mode
  • These are also related to GUI. It sets the colors, font etc. fg means Foreground and bg means background.
  • playlist.grid()locates widgets in a two dimensional grid using row and column absolute coordinates.
os.chdir(r'D:\MyPlayList')

playlist=Listbox(root,selectmode=SINGLE,bg="DodgerBlue2",fg="white",font=('arial',15),width=40)
playlist.grid(columnspan=5)
songs=os.listdir()
for s in songs:
    playlist.insert(END,s)
Enter fullscreen mode Exit fullscreen mode
  • os.chdirmethod in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.
  • os.listdr() method in python is used to get the list of all files and directories in the specified directory. If we don't specify any directory, then list of files and directories in the current working directory will be returned.
  • In for loop, it inserts all the files in our file directory to the playlist.
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()
Enter fullscreen mode Exit fullscreen mode

This is the last part. All of these lines are for GUI Buttons.

  • command is for to command. The comman name is the functions name that we wrote first. Eg : playsong. resumesong We talked about all other GUI variables in above.

So finally, you have made your own music player with Python! Congrats buddy!

Get Full Code

And the result will be,

image.png

Since we want only the music player, don't care much about GUI.πŸ˜‰

Happy Coding!

Originally published on Hashnode

Top comments (0)