Project: MP3 Player with Python GUI


This project is a simple and interactive MP3 player built using Python. The player allows users to load an MP3 file, play it, and stop it conveniently through an easy-to-use graphical user interface (GUI). Here's a breakdown of how the project works and how you can build it.


Features:


1. Select MP3 File: Upload any MP3 file from your computer.



2. Play Audio: Click the "Play Audio" button to listen to the selected file.



3. Stop Audio: Stop playback at any time with the "Stop Audio" button.



4. User-Friendly Interface: The application has an intuitive layout, making it ideal for beginners.





---


Prerequisites:


Python 3.x installed on your system.


Required libraries:


pygame for audio playback.


tkinter for building the GUI.




You can install pygame with the command:


pip install pygame



---


Code:


import tkinter as tk

from tkinter import filedialog

import pygame


# Initialize Pygame mixer

pygame.mixer.init()


# Global variable to store file path

selected_file = None


# Function to select an MP3 file

def select_file():

    global selected_file

    selected_file = filedialog.askopenfilename(filetypes=[("MP3 Files", "*.mp3")])

    if selected_file:

        status_label.config(text="File selected: " + selected_file.split("/")[-1])


# Function to play the selected MP3 file

def play_audio():

    if selected_file:

        pygame.mixer.music.load(selected_file)

        pygame.mixer.music.play()

        status_label.config(text="Playing: " + selected_file.split("/")[-1])

    else:

        status_label.config(text="Please select a file first.")


# Function to stop audio playback

def stop_audio():

    pygame.mixer.music.stop()

    status_label.config(text="Playback stopped.")


# Create GUI

def create_gui():

    root = tk.Tk()

    root.title("MP3 Player")

    root.geometry("400x200")


    tk.Label(root, text="MP3 Player", font=("Arial", 16)).pack(pady=10)


    select_button = tk.Button(root, text="Select MP3 File", command=select_file, bg="blue", fg="white", font=("Arial", 12))

    select_button.pack(pady=5)


    play_button = tk.Button(root, text="Play Audio", command=play_audio, bg="green", fg="white", font=("Arial", 12))

    play_button.pack(pady=5)


    stop_button = tk.Button(root, text="Stop Audio", command=stop_audio, bg="red", fg="white", font=("Arial", 12))

    stop_button.pack(pady=5)


    global status_label

    status_label = tk.Label(root, text="No file selected", font=("Arial", 10))

    status_label.pack(pady=10)


    root.mainloop()


# Run GUI

if __name__ == "__main__":

    create_gui()



---


How It Works:


1. Load the Application: Start the app, and you will see three buttons: Select MP3 File, Play Audio, and Stop Audio.



2. Choose a File: Click Select MP3 File to browse and choose an MP3 file.



3. Play the File: Click Play Audio to start listening.



4. Stop Playback: Click Stop Audio to end the playback anytime.





---


This project is an excellent starting point for learning Python GUI programming. Expa

nd it further by adding volume control, a playlist feature, or visualizations!


Image Credit: [Attached GUI Illustration]


Comments

Popular posts from this blog