Create a Music Player Using Python

Music player

It’s a favorite project of beginner programmers.

Of course, it is easy to code, if you follow this article properly.

Anime Music Player GUI Application

Anime and Manga have one huge difference between them, which puts Anime in advantage. This huge difference is Anime Opening and Soundtracks. Anime Openings, endings, and soundtracks are something you shouldn’t skip.

Image by Author

In contrast, let’s get started and create our own Anime Player.

How To Create A Music Player?

Creating your own Anime Player using Python is quite simple. Before getting into it, you need to install the required Python libraries. Since this project is based on GUI music, you will need to install Python Tkinter and Pygame.

pip install python-tk
pip install pygame
pip install pydub  #to convert .mp3 to .ogg

Create a root window by specifying the dimensions. Tkinter provides various widgets and we are free to use every single one of them. Make use of a LabelFrame, which will display the List of Anime Openings, OST, and endings.

Here you have to provide the file name where you have stored the Anime Openings. In order to change the directory, you need to import the os module. Now use the below command to switch the directory and filename.

import os

os.chdir("AnimeTrack") #change the directory
anime_songs = os.listdir() #store all songs in a list
anime_songs.sort()
for songs in anime_songs[:-1]:
    #add all songs in the ListBox
    animelist.insert(tk.END,songs)

Tkinter provides ListBox, after changing the directory/file insert all the openings/OSTs in the ListBox. In addition, configure a scrollbar on the y axis so that you can scroll up/down to play the music.

#configure the scroll-bar which becomes easy to choose a song

scroll_bar = tk.Scrollbar(anime_frame,orient="vertical") #on y-axis
animelist=tk.Listbox(anime_frame,yscrollcommand=scroll_bar.set,selectbackground="white")
scroll_bar.pack(side="right",fill="y")
scroll_bar.config(command=animelist.yview)
animelist.pack(fill="both")

LabelFrame, ListBox, and Scrollbar are the widgets of Tkinter that we have used so far. Now download the play, pause, stop and resume images. Add these images as buttons to perform the trigger function. This is self-explanatory, create a play_song function for the play button and do the same for the rest buttons.

Now that the front end is created, how do you play and pause the songs?

How to play and pause audio using Pygame?

We have programmed the frontend, what now remains is playing the music in the backend. As we have used 4 buttons each for play, pause, stop and resume, we will require 4 different functions. All things considered, let’s import the pygame module. First import pygame, Second initialize pygame.mixer, Third load the song to be played.

One should be noted that the mp3 song format is not supported by pygame. With this in mind convert the mp3 file to the .ogg/.wav file. For this reason, you need to import the pydub library.

For every image button to create a function, you can do this by using the command attribute. It’s a Music Player and you are supposed to play the Music which is clicked. However, there is one such specialty of ListBox, that provides many features one of which includes Active. With help of this, you can perform pygame.mixer operations on active songs.

Here is the syntax to play, pause, stop and resume the active audio.

Finally, we have covered all the steps but we have missed one thing. Last but not least, is to add the Status Frame to the Music Player. Status Frame is used to display the song name which is active with a status. Playing is the status when the song is in the loop, the rest is common.

Anime Openings and Original Soundtracks

There is no way you can skip any song unless you finish it, that’s how it works. No skipping the Anime Openings.

Music Player using Python is successfully completed. Last but not the least, the most important is downloading the songs if you don’t have any. We know how to change the directory using the os module. But there’s no use in switching directory when you don’t have any Anime music.

You can download few Anime Openings and save them in your music directory. Keeping the storage issue in mind, I limited the count to 20. Of course, choosing only 20 songs was difficult. You can add more songs in your local directory and use the directory to display the songs in ListBox. Download the Anime Opening from my repo.

Conclusion

Creating your own Music Player is like designing your own playlist. You can even add a background image using the Tkinter Canvas widget. Explore more and re-design the above Anime Music Player into your own style.

Download the source code for the above project.

Do you like the Anime Vyuh Content? Support us by buying a coffee.