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
- Make sure you have Python installed on your system. You can download it from here.
- Install the
pygamelibrary if it is not already installed. Run the following command in your terminal or command prompt:pip install pygame
- Save the provided Python code into a file named
music_player.py. - Run the program from the terminal or command prompt using the following command:
python music_player.py
- The music player window will open, and you can start using the buttons to play, pause, and stop music.

