Python

 

Introduction:

The objective of this project is to create a basic music player application using Python. This program will allow the user to play, pause, and stop audio files. We will use the `pygame` library to handle the audio functionality, which simplifies the task of creating a music player.

In this tutorial, you will learn how to integrate Python and the pygame library to build a basic music player that can be used to play music files in the popular MP3 format.

Program Code

import pygame
import tkinter as tk
from tkinter import filedialog

# Initialize pygame mixer for playing audio
pygame.mixer.init()

# Function to play the selected music
def play_music():
    file = filedialog.askopenfilename(title="Select a music file", filetypes=(("MP3 files", "*.mp3"), ("All files", "*.*")))
    if file:
        pygame.mixer.music.load(file)
        pygame.mixer.music.play()

# Function to stop the music
def stop_music():
    pygame.mixer.music.stop()

# Function to pause the music
def pause_music():
    pygame.mixer.music.pause()

# Function to unpause the music
def unpause_music():
    pygame.mixer.music.unpause()

# Create the main window for the music player
window = tk.Tk()
window.title("Basic Music Player")

# Adding buttons for play, stop, pause, and unpause functionality
play_button = tk.Button(window, text="Play", width=20, command=play_music)
play_button.pack(pady=10)

pause_button = tk.Button(window, text="Pause", width=20, command=pause_music)
pause_button.pack(pady=10)

unpause_button = tk.Button(window, text="Unpause", width=20, command=unpause_music)
unpause_button.pack(pady=10)

stop_button = tk.Button(window, text="Stop", width=20, command=stop_music)
stop_button.pack(pady=10)

# Run the application
window.mainloop()

Explanation of the Program Structure

This program consists of several key components:

  • pygame.mixer: This is the module used to load and play music files.
  • tkinter: We are using the tkinter library to create a simple graphical user interface (GUI) for the music player with buttons to control the music.
  • play_music function: Opens a file dialog allowing the user to select a music file and plays it using the pygame.mixer library.
  • stop_music function: Stops the music if it is currently playing.
  • pause_music function: Pauses the music.
  • unpause_music function: Resumes playing the music from where it was paused.

The graphical user interface is simple. There are four buttons: Play, Pause, Unpause, and Stop. These buttons are mapped to their respective functions that interact with the pygame mixer to control audio playback.

How to Run the Program

  1. Make sure you have Python installed on your system. You can download it from here.
  2. Install the pygame library if it is not already installed. Run the following command in your terminal or command prompt:
    pip install pygame
  3. Save the provided Python code into a file named music_player.py.
  4. Run the program from the terminal or command prompt using the following command:
    python music_player.py
  5. The music player window will open, and you can start using the buttons to play, pause, and stop music.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)