Basic Music Player
Abstract
Create a fully functional music player application with a graphical user interface that supports playlist management, playback controls (play, pause, stop, resume), directory loading, and visual feedback. This project combines GUI development with audio processing capabilities.
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
- Basic understanding of Python syntax
- Knowledge of Tkinter for GUI development
- Familiarity with file system operations
- Understanding of event-driven programming
Getting Started
Create a new project
- Create a new project folder and name it
basicMusicPlayer
basicMusicPlayer
. - Create a new file and name it
basicmusicplayer.py
basicmusicplayer.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into your
basicmusicplayer.py
basicmusicplayer.py
file.
Install required dependencies
- Install pygame for audio handling:
pip install pygame
pip install pygame
Write the code
- Add the following code to your
basicmusicplayer.py
basicmusicplayer.py
file.
⚙️ Basic Music Player
# Basic Music Player
# Credit: https://pythongeeks.org/python-music-player/
# Import Modules
from tkinter import * # pip install tkinter
from tkinter import filedialog
import pygame.mixer as mixer # pip install pygame
import os
# Initializing the mixer
mixer.init()
# Creating the master GUI for python music player
root = Tk()
root.geometry('700x220')
root.title('Basic Music Player')
root.resizable(0, 0)
# Play, Stop, Load and Pause & Resume functions
def play_song(song_name: StringVar, songs_list: Listbox, status: StringVar):
song_name.set(songs_list.get(ACTIVE))
mixer.music.load(songs_list.get(ACTIVE))
mixer.music.play()
status.set("Song PLAYING")
def stop_song(status: StringVar):
mixer.music.stop()
status.set("Song STOPPED")
def load(listbox):
os.chdir(filedialog.askdirectory(title='Open a songs directory'))
tracks = os.listdir()
for track in tracks:
listbox.insert(END, track)
def pause_song(status: StringVar):
mixer.music.pause()
status.set("Song PAUSED")
def resume_song(status: StringVar):
mixer.music.unpause()
status.set("Song RESUMED")
# All the frames
song_frame = LabelFrame(root, text='Current Song', bg='LightBlue', width=400, height=80)
song_frame.place(x=0, y=0)
button_frame = LabelFrame(root, text='Control Buttons', bg='Turquoise', width=400, height=120)
button_frame.place(y=80)
listbox_frame = LabelFrame(root, text='Playlist', bg='RoyalBlue')
listbox_frame.place(x=400, y=0, height=200, width=300)
# All StringVar variables
current_song = StringVar(root, value='<Not selected>')
song_status = StringVar(root, value='<Not Available>')
# Playlist ListBox
playlist = Listbox(listbox_frame, font=('Helvetica', 11), selectbackground='Gold')
scroll_bar = Scrollbar(listbox_frame, orient=VERTICAL)
scroll_bar.pack(side=RIGHT, fill=BOTH)
playlist.config(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=playlist.yview)
playlist.pack(fill=BOTH, padx=5, pady=5)
# SongFrame Labels
Label(song_frame, text='CURRENTLY PLAYING:', bg='LightBlue', font=('Times', 10, 'bold')).place(x=5, y=20)
song_lbl = Label(song_frame, textvariable=current_song, bg='Goldenrod', font=("Times", 12), width=25)
song_lbl.place(x=150, y=20)
# Buttons in the main screen
pause_btn = Button(button_frame, text='Pause', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: pause_song(song_status))
pause_btn.place(x=15, y=10)
stop_btn = Button(button_frame, text='Stop', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: stop_song(song_status))
stop_btn.place(x=105, y=10)
play_btn = Button(button_frame, text='Play', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: play_song(current_song, playlist, song_status))
play_btn.place(x=195, y=10)
resume_btn = Button(button_frame, text='Resume', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: resume_song(song_status))
resume_btn.place(x=285, y=10)
load_btn = Button(button_frame, text='Load Directory', bg='Aqua', font=("Georgia", 13), width=35, command=lambda: load(playlist))
load_btn.place(x=10, y=55)
# Label at the bottom that displays the state of the music
Label(root, textvariable=song_status, bg='SteelBlue', font=('Times', 9), justify=LEFT).pack(side=BOTTOM, fill=X)
# Finalizing the GUI
root.update()
root.mainloop()
# Basic Music Player
# Credit: https://pythongeeks.org/python-music-player/
# Import Modules
from tkinter import * # pip install tkinter
from tkinter import filedialog
import pygame.mixer as mixer # pip install pygame
import os
# Initializing the mixer
mixer.init()
# Creating the master GUI for python music player
root = Tk()
root.geometry('700x220')
root.title('Basic Music Player')
root.resizable(0, 0)
# Play, Stop, Load and Pause & Resume functions
def play_song(song_name: StringVar, songs_list: Listbox, status: StringVar):
song_name.set(songs_list.get(ACTIVE))
mixer.music.load(songs_list.get(ACTIVE))
mixer.music.play()
status.set("Song PLAYING")
def stop_song(status: StringVar):
mixer.music.stop()
status.set("Song STOPPED")
def load(listbox):
os.chdir(filedialog.askdirectory(title='Open a songs directory'))
tracks = os.listdir()
for track in tracks:
listbox.insert(END, track)
def pause_song(status: StringVar):
mixer.music.pause()
status.set("Song PAUSED")
def resume_song(status: StringVar):
mixer.music.unpause()
status.set("Song RESUMED")
# All the frames
song_frame = LabelFrame(root, text='Current Song', bg='LightBlue', width=400, height=80)
song_frame.place(x=0, y=0)
button_frame = LabelFrame(root, text='Control Buttons', bg='Turquoise', width=400, height=120)
button_frame.place(y=80)
listbox_frame = LabelFrame(root, text='Playlist', bg='RoyalBlue')
listbox_frame.place(x=400, y=0, height=200, width=300)
# All StringVar variables
current_song = StringVar(root, value='<Not selected>')
song_status = StringVar(root, value='<Not Available>')
# Playlist ListBox
playlist = Listbox(listbox_frame, font=('Helvetica', 11), selectbackground='Gold')
scroll_bar = Scrollbar(listbox_frame, orient=VERTICAL)
scroll_bar.pack(side=RIGHT, fill=BOTH)
playlist.config(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=playlist.yview)
playlist.pack(fill=BOTH, padx=5, pady=5)
# SongFrame Labels
Label(song_frame, text='CURRENTLY PLAYING:', bg='LightBlue', font=('Times', 10, 'bold')).place(x=5, y=20)
song_lbl = Label(song_frame, textvariable=current_song, bg='Goldenrod', font=("Times", 12), width=25)
song_lbl.place(x=150, y=20)
# Buttons in the main screen
pause_btn = Button(button_frame, text='Pause', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: pause_song(song_status))
pause_btn.place(x=15, y=10)
stop_btn = Button(button_frame, text='Stop', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: stop_song(song_status))
stop_btn.place(x=105, y=10)
play_btn = Button(button_frame, text='Play', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: play_song(current_song, playlist, song_status))
play_btn.place(x=195, y=10)
resume_btn = Button(button_frame, text='Resume', bg='Aqua', font=("Georgia", 13), width=7,
command=lambda: resume_song(song_status))
resume_btn.place(x=285, y=10)
load_btn = Button(button_frame, text='Load Directory', bg='Aqua', font=("Georgia", 13), width=35, command=lambda: load(playlist))
load_btn.place(x=10, y=55)
# Label at the bottom that displays the state of the music
Label(root, textvariable=song_status, bg='SteelBlue', font=('Times', 9), justify=LEFT).pack(side=BOTTOM, fill=X)
# Finalizing the GUI
root.update()
root.mainloop()
- Save the file.
- Prepare some music files in a folder.
- Run the following command to run the application.
C:\Users\username\Documents\basicMusicPlayer> python basicmusicplayer.py
[Music Player GUI Opens]
- Click "Load" to browse and select music folder
- Select songs from the playlist
- Use Play, Pause, Stop, Resume buttons
- Current song and status displayed at top
C:\Users\username\Documents\basicMusicPlayer> python basicmusicplayer.py
[Music Player GUI Opens]
- Click "Load" to browse and select music folder
- Select songs from the playlist
- Use Play, Pause, Stop, Resume buttons
- Current song and status displayed at top
-
Install Required Dependencies
pip install pygame # tkinter comes built-in with Python
pip install pygame # tkinter comes built-in with Python
-
Prepare Music Files
- Create a folder with music files (MP3, WAV, etc.)
- Ensure files are in supported audio formats
-
Run the Music Player
python basicmusicplayer.py
python basicmusicplayer.py
-
Load Music Directory
- Click “Load Directory” button
- Select folder containing your music files
Explanation
- The
import pygame.mixer as mixer
import pygame.mixer as mixer
statement imports the Pygame mixer module for audio playback. - The
mixer.init()
mixer.init()
function initializes the audio system for playing music files. - The
from tkinter import *
from tkinter import *
imports all Tkinter components for creating the GUI. - The
from tkinter import filedialog
from tkinter import filedialog
imports the file dialog for browsing directories. - The
song_frame = LabelFrame()
song_frame = LabelFrame()
creates a labeled frame to display the current song. - The
button_frame = LabelFrame()
button_frame = LabelFrame()
creates a frame for organizing control buttons. - The
listbox_frame = LabelFrame()
listbox_frame = LabelFrame()
creates a frame for the playlist display. - The
def play_song()
def play_song()
function handles music playback and status updates. - The
def pause_song()
def pause_song()
function pauses the current song and updates status. - The
def load()
def load()
function opens a directory browser to load music files. - The
playlist = Listbox()
playlist = Listbox()
creates a scrollable list for managing songs. - The control buttons (Play, Pause, Stop, Resume) provide music player functionality.
Next Steps
Congratulations! You have successfully created a Basic Music Player in Python. Experiment with the code and see if you can modify the application. Here are a few suggestions:
- Add volume control slider
- Implement shuffle and repeat modes
- Add song information display (duration, artist, album)
- Create playlist saving and loading functionality
- Add keyboard shortcuts for controls
- Implement music visualization
- Add support for more audio formats
- Create custom themes and skins
- Add favorite songs functionality
Conclusion
In this project, you learned how to create a Basic Music Player in Python using Tkinter and Pygame. You also learned about GUI development, audio processing, and event-driven programming. You can find the source code on GitHub
Was this page helpful?
Let us know how we did