Make Your Own Music Player Using Python 🎧
Tags: python, tutorial, beginners, programming
Published: 2021-11-01 12:33:22


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](https://cdn.hashnode.com/res/hashnode/image/upload/v1621244609273/TJd9P71Rt.png)

## Prerequisites
- Knowledge about Python Basics
- Install Tkinter and PyGame. Then, we have to import modules.
```python
import pygame
from pygame import mixer
from tkinter import *
import os
```

## Coding Time
Your most loving part. Let's start!

```python
def playsong():
    currentsong=playlist.get(ACTIVE)
    print(currentsong)
    mixer.music.load(currentsong)
    songstatus.set("Playing")
    mixer.music.play()
```

 - `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'.

```python
def pausesong():
    songstatus.set("Paused")
    mixer.music.pause()

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

def resumesong():
    songstatus.set("Resuming")
    mixer.music.unpause()
```
- 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.

```python
root=Tk()
root.title('Buddy Music player')

mixer.init()
songstatus=StringVar()
songstatus.set("choosing")
```
- In here, `root` is the main GUI window. `root.title` set a title to the window. (Change it as you want;)

```python
playlist=Listbox(root,selectmode=SINGLE,bg="DodgerBlue2",fg="white",font=('arial',15),width=40)
playlist.grid(columnspan=5)
```

- 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. 

```python
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)
```

- `os.chdir`method 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`.

```python
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()
```
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](https://github.com/SmallLion/Python-Projects/blob/main/Music_player.py)


And the result will be,

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1621244386713/TYHU6_MYu.png)

Since we want only the music player, don't care much about GUI.😉 

Happy Coding!

[Originally](https://mr-unity-buddy.hashnode.dev/make-your-own-music-player-using-python) published on Hashnode