Creating a Music Player in Python using VLC and Tkinter

A boy wearing headphones sits at a table, listening to music, displaying a text describing a music player in python next to it.

Introduction

Python, with its vast array of libraries and frameworks, allows developers to build a wide range of applications. One such application is a music player, which can be developed using the Tkinter library for the graphical user interface (GUI) and the VLC library for playing audio files. In this article, we will explore how to create a music player with a playlist feature using these two libraries.

Prerequisites

Before we begin, make sure you have Python installed on your system. Additionally, you’ll need to install the following libraries:

1. Tkinter: It is the standard Python interface to the Tk GUI toolkit.

2. VLC: It provides a Python binding for the VLC media player.

To install the libraries, you can use the pip package manager by running the following commands in your terminal or command prompt:


pip install tkinter
pip install python-vlc

Once you have installed the required libraries, let’s move on to building our music player.

Step 1: Import the necessary libraries

Start by importing the required libraries into your Python script:


import os
import vlc
import tkinter as tk
from tkinter import filedialog

Step 2: Create the MusicPlayer class

Next, we will define a class called MusicPlayer that encapsulates the functionality of our music player:


class MusicPlayer:
    def __init__(self, window):
        self.window = window
        self.window.title("Music Player")
        self.window.geometry("500x340")

        # Create the playlist
        self.playlist = tk.Listbox(self.window, width=50)
        self.playlist.pack(pady=10)

        # Create the controls frame
        controls_frame = tk.Frame(self.window)
        controls_frame.pack()

        # Create the play button
        self.play_button = tk.Button(controls_frame, text="Play", command=self.play)
        self.play_button.grid(row=0, column=0, padx=10)

        # Create the pause button
        self.pause_button = tk.Button(controls_frame, text="Pause", command=self.pause)
        self.pause_button.grid(row=0, column=1, padx=10)

        # Create the stop button
        self.stop_button = tk.Button(controls_frame, text="Stop", command=self.stop)
        self.stop_button.grid(row=0, column=2, padx=10)

        # Create the add button
        self.add_button = tk.Button(controls_frame, text="Add", command=self.add_to_playlist)
        self.add_button.grid(row=1, column=0, pady=10)

        # Create the remove button
        self.remove_button = tk.Button(controls_frame,text="Remove",command=self.remove_song)
        self.remove_button.grid(row=1, column=1, pady=10)

        # Create the vlc player instance
        self.player = vlc.Instance()
        self.media_player = self.player.media_player_new()

        # Set the end event
        the_event = vlc.EventType.MediaPlayerEndReached
        self.media_player.event_manager().event_attach(the_event, self.next_song)

Step 3: Implement the player controls

In the MusicPlayer class, add the following methods to implement the player controls:


    def play(self):
        selected_song = self.playlist.get(tk.ACTIVE)
        media = self.player.media_new(selected_song)
        self.media_player.set_media(media)
        self.media_player.play()

    def pause(self):
        self.media_player.pause()

    def stop(self):
        self.media_player.stop()

    def next_song(self, event):
        next_index = (self.playlist.curselection()[0] + 1) % self.playlist.size()
        self.playlist.selection_clear(0, tk.END)
        self.playlist.activate(next_index)
        self.playlist.selection_set(next_index)
        self.play()

    def add_to_playlist(self):
        file_path = filedialog.askopenfilename(defaultextension=".mp3",
        filetypes=[("MP3 Files", "*.mp3"), ("WAV Files", "*.wav")])
        
        if file_path:
            self.playlist.insert(tk.END, file_path)

    def remove_song(self):
        selected_index = self.playlist.curselection()[0]
        self.playlist.delete(selected_index)

Step 4: Initialize the music player

Finally, initialize an instance of the MusicPlayer class and start the main event loop:


if __name__ == "__main__":
    window = tk.Tk()
    music_player = MusicPlayer(window)
    window.mainloop()

That’s it! You have successfully created a music player with playlist functionality using Tkinter and the VLC library in Python. You can now run the script, and a GUI window will appear with the player controls and playlist. You can add songs to the playlist, play, pause, stop, and remove songs from the playlist.

Output 

Screenshot of a Python music player application with a playlist feature. The interface displays playlist, buttons for adding/removing music to the playlist, play, pause, and stop options.
Music Player Application

This is just the tip of the iceberg, as there are countless possibilities for expanding and enhancing this music player. Feel free to enhance the functionality of the music player further by adding features like volume control, seeking, or implementing a graphical representation of the currently playing song.

Happy coding and enjoy listening to your favorite tunes with your new music player!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147