MP3 Player using Python
KISHLAY KUMAR - 1BI19CS076
MRITYUNJAY PRAKASH – 1BI19CS091
NITISH KUMAR – 1BI19CS102
RAHUL KUMAR SAINI – 1BI19CS118
RAM KUMAR PANDEY – 1BI19CS123
ROHIT RANJAN – 1BI19CS130
Introduction
The main objective of this project is to design cross-platform music player using python and
tkinter .
Python is a very Interesting language because this language provides usability to write cross-
platform codes that can run on all types of the system without even doing any big changes in codes.
This Python module provides a high-level core Music player interface where you are supposed to
provide all the remaining high-level logic like the user interface, the playlist logic and the audio
data.
Abstract
Audio is an important source of communication and this music player is used to play music and
other digital audio files.
The music player GUI project idea tries to emulate the physical music player.
This program allows you to play songs, music and all music files on your desktop or laptop.
This application is a basic programming application built using the Python programming language.
It is a GUI program created using the Python Tkinter pygame libraries.
The music player application must be able to play a song, create and display a playlist, pause and
resume a long song, change the song, play the previous or next song.
Python Graphical User Interfaces (GUIs)
There are three major options available for Python Interfaces :
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python. We would look this option in
this chapter.
wxPython − This is an open-source Python interface for wxWindows.
JPython − JPython is a Python port for Java which gives Python scripts seamless access to Java class libraries on
the local machine.
Product feature
PLAY : We added play option for playing the song which is imported from library of
system.
PAUSE : To pause the recently playing music.
PLAYLIST : In this we have given option to add songs from system.
Volume : We can change the volume of the audio by using the slider which ranges from 0-
10.
Algorithm
Import the libraries.
Create an object of the Tkinter and Pygame libraries.
Create a window using Tkinter object.
Add buttons that provide different functionalities :
Add a playlist, Play a song, Pause the song, Play previous song, Play next song,
Change the volume
Contd..
Add a song button when pressed should open a dialog box to browse and choose the file.
Add label to display the song’s information.
Display screen will display the details of the entire playlist.
Close button will automatically clear the song list and will stop playing.
Flow Chart
Modules
Tkinter
Tkinter library is a standard library for GUI creation.
The Tkinter library is the most popular and easiest to use and contains many widgets (these
widgets help create attractive GUI applications).
Tkinter is a very lightweight module and useful for building cross-platform applications (so the
same code can be easily used on Windows, macOS, and Linux.
Developing desktop based applications with python Tkinter is not a complex task. An empty
Tkinter top-level window can be created by using the following steps.
Import the Tkinter module.
Create the main application window.
Add the widgets like labels, buttons, frames, etc. to the window.
Call the main event loop so that the actions can take place on the
Pygame
Pygame is a Python module that works with computer graphics and sound libraries and designed
with the power of playing with different multimedia formats like audio, video, etc.
Pygame is a cross-platform set of Python modules which is used to create video games.
Pygame was officially written by Pete Shinners to replace PySDL.
Pygame is suitable to create client-side applications that can be potentially wrapped in a standalone
executable.
OS module
Python OS module provides the facility to establish the interaction between the user and the operating
system.
It offers many useful OS functions that are used to perform OS-based tasks and get related information
about operating system.
The OS comes under Python's standard utility modules. This module offers a portable way of using
operating system dependent functionality.
The Python OS module lets us work with the files and directories.
In this project, we will use the OS module to get the song playlist from the specified directory and make it
available to the music player application.
Pickle
Python pickle module is used for serializing and de-serializing a Python object structure.
Any object in Python can be pickled so that it can be saved on disk. What pickle does is
that it “serializes” the object first before writing it to file.
Pickling is a way to convert a python object (list, dict, etc.) into a character stream.
The idea is that this character stream contains all the information necessary to reconstruct
the object in another python script.
Widgets
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI
application. These controls are commonly called widgets.
Some of the widgets used in the project are:
Button - The Button widget is used to display buttons in your application.
Canvas - The Canvas widget is used to draw shapes, such as lines, ovals, polygons and
rectangles, in your application.
Checkbutton - The Checkbutton widget is used to display a number of options as
checkboxes. The user can select multiple options at a time.
Photoimage - Widget which can display images in PGM, PPM, GIF, PNG format.
Entry - The Entry widget is used to display a single-line text field for accepting values
from a user.
Frame - The Frame widget is used as a container widget to organize other widgets.
Label - The Label widget is used to provide a single-line caption for other widgets. It can
also contain images.
Message - The Message widget is used to display multiline text fields for accepting values
from a user.
Scale - The Scale widget is used to provide a slider widget.
ScrollBar - The Scrollbar widget is used to add scrolling capability to various widgets,
such as list boxes.
LabelFrame - A labelframe is a simple container widget. Its primary purpose is to act as a
spacer or container for complex window layouts.
tkMessageBox - This module is used to display message boxes in your applications.
Code
import os
import pickle
import tkinter as tk
from tkinter import filedialog
from tkinter import PhotoImage
from pygame import mixer
Class Player(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master = master
self.pack()
mixer.init()
if os.path.exists('songs.pickle'):
with open('songs.pickle', 'rb') as f:
self.playlist = pickle.load(f)
else:
self.playlist=[]
self.current = 0
self.paused = True
self.played = False
self.create_frames()
self.track_widgets()
self.control_widgets()
self.tracklist_widgets()
def create_frames(self):
self.track = tk.LabelFrame(self, text='Song Track',
font=("times new roman",15,"bold"),
bg="grey",fg="white",bd=5,relief=tk.GROOVE)
self.track.config(width=410,height=300)
self.track.grid(row=0, column=0, padx=10)
self.tracklist = tk.LabelFrame(self, text=f'PlayList - {str(len(self.playlist))}',
font=("times new roman",15,"bold"),
bg="grey",fg="white",bd=5,relief=tk.GROOVE)
self.tracklist.config(width=190,height=400)
self.tracklist.grid(row=0, column=1, rowspan=3, pady=5)
self.controls = tk.LabelFrame(self,
font=("times new roman",15,"bold"),
bg="white",fg="white",bd=2,relief=tk.GROOVE)
self.controls.config(width=410,height=80)
self.controls.grid(row=2, column=0, pady=5, padx=10)
def track_widgets(self):
self.canvas = tk.Label(self.track, image=img)
self.canvas.configure(width=400, height=240)
self.canvas.grid(row=0,column=0)
self.songtrack = tk.Label(self.track, font=("times new roman",16,"bold"),
bg="white",fg="dark blue")
self.songtrack['text'] = 'Musicxy MP3 Player'
self.songtrack.config(width=30, height=1)
self.songtrack.grid(row=1,column=0,padx=10)
def control_widgets(self):
self.loadSongs = tk.Button(self.controls, bg='green', fg='white', font=10)
self.loadSongs['text'] = 'Load Songs'
self.loadSongs['command'] = self.retrieve_songs
self.loadSongs.grid(row=0, column=0, padx=10)
self.prev = tk.Button(self.controls, image=prev)
self.prev['command'] = self.prev_song
self.prev.grid(row=0, column=1)
self.pause = tk.Button(self.controls, image=pause)
self.pause['command'] = self.pause_song
self.pause.grid(row=0, column=2)
self.next = tk.Button(self.controls, image=next_)
self.next['command'] = self.next_song
self.next.grid(row=0, column=3)
self.volume = tk.DoubleVar(self)
self.slider = tk.Scale(self.controls, from_ = 0, to = 10, orient = tk.HORIZONTAL)
self.slider['variable'] = self.volume
self.slider.set(8)
mixer.music.set_volume(0.8)
self.slider['command'] = self.change_volume
self.slider.grid(row=0, column=4, padx=5)
def tracklist_widgets(self):
self.scrollbar = tk.Scrollbar(self.tracklist, orient=tk.VERTICAL)
self.scrollbar.grid(row=0,column=1, rowspan=5, sticky='ns')
self.list = tk.Listbox(self.tracklist, selectmode=tk.SINGLE,
yscrollcommand=self.scrollbar.set, selectbackground='sky blue')
self.enumerate_songs()
self.list.config(height=22)
self.list.bind('<Double-1>', self.play_song)
self.scrollbar.config(command=self.list.yview)
self.list.grid(row=0, column=0, rowspan=5)
def retrieve_songs(self):
self.songlist = []
directory = filedialog.askdirectory()
for root_, dirs, files in os.walk(directory):
for file in files:
if os.path.splitext(file)[1] == '.mp3':
path = (root_ + '/' + file).replace('\\','/')
self.songlist.append(path)
with open('songs.pickle', 'wb') as f:
pickle.dump(self.songlist, f)
self.playlist = self.songlist
self.tracklist['text'] = f'PlayList - {str(len(self.playlist))}'
self.list.delete(0, tk.END)
self.enumerate_songs()
def enumerate_songs(self):
for index, song in enumerate(self.playlist):
self.list.insert(index, os.path.basename(song))
def play_song(self, event=None):
if event is not None:
self.current = self.list.curselection()[0]
for i in range(len(self.playlist)):
self.list.itemconfigure(i, bg="white")
print(self.playlist[self.current])
mixer.music.load(self.playlist[self.current])
self.songtrack['anchor'] = 'w'
self.songtrack['text'] = os.path.basename(self.playlist[self.current])
self.pause['image'] = play
self.paused = False
self.played = True
self.list.activate(self.current)
self.list.itemconfigure(self.current, bg='sky blue')
mixer.music.play()
def pause_song(self):
if not self.paused:
self.paused = True
mixer.music.pause()
self.pause['image'] = pause
else:
if self.played == False:
self.play_song()
self.paused = False
mixer.music.unpause()
self.pause['image'] = play
def prev_song(self):
if self.current > 0:
self.current -= 1
else:
self.current = 0
self.list.itemconfigure(self.current + 1, bg='white')
self.play_song()
def next_song(self):
if self.current < len(self.playlist) - 1:
self.current += 1
else:
self.current = 0
self.list.itemconfigure(self.current - 1, bg='white')
self.play_song()
def change_volume(self, event=None):
self.v = self.volume.get()
mixer.music.set_volume(self.v / 10)
# ----------------------------- Main -------------------------------------------
root = tk.Tk()
file = ''
root.geometry('600x400')
root.wm_title('Group-V')
img = PhotoImage(file='images/music.gif')
next_ = PhotoImage(file = 'images/next.gif')
prev = PhotoImage(file='images/previous.gif')
play = PhotoImage(file='images/play.gif')
pause = PhotoImage(file='images/pause.gif')
app = Player(master=root)
app.mainloop()
Conclusion
The MP3 player is a device for playing and listening to digital audio files, which can be MP3 files
or other audio files.
The player was created in Python language. A GUI implementation of the application has been
developed that is simple and easy to use.
The application gives the user five options: add a song to a playlist, play the song, pause or resume
the song, play the previous song, and play the next song.
Contd..
The player can also add multiple tracks to the playlist at the same time. It has a large display
area in which the playlist is visible.
Once a track has been selected and played we can listen to it and view details, the song is at the
top of the screen.
This information includes details about the song.
Thank You