Skip to content

codehelping.com

Best Platform to learn AI & Coding

  • Home
  • Projects
  • Notes
  • Blog
  • Contact Us

How to Build a Simple Music Player in Python | CodeHelping

Posted on August 19, 2025August 19, 2025 By Omkar Pathak 2 Comments on How to Build a Simple Music Player in Python | CodeHelping
Blog

In this blog we will learn how to build a simple music player in Python using libraries: Tkinter for the user interface and Pygame mixer for handling the sound. Don’t worry if you are a beginner, I will explain step by step in simple words.

How to Build a Simple Music Player in Python | CodeHelping

Code: Build a Simple Music Player in Python

import tkinter as tk
from pygame import mixer

# Create main window
root = tk.Tk()
root.title("Music Player")
root.geometry("600x300")

# Initialize mixer
mixer.init()
mixer.music.load("song.mp3")  # replace with your file

# Functions
def play():
    mixer.music.play()

def pause():
    mixer.music.pause()

def resume():
    mixer.music.unpause()

def stop():
    mixer.music.stop()

# UI
title = tk.Label(root, text="Welcome to Music Player", font=("Lucida", 24, "bold"))
title.pack(pady=20)

btn_play = tk.Button(root, text="Play", width=8, command=play)
btn_play.place(x=200, y=100)

btn_pause = tk.Button(root, text="Pause", width=8, command=pause)
btn_pause.place(x=270, y=100)

btn_resume = tk.Button(root, text="Resume", width=8, command=resume)
btn_resume.place(x=350, y=100)

btn_stop = tk.Button(root, text="Stop", width=8, command=stop)
btn_stop.place(x=430, y=100)

btn_quit = tk.Button(root, text="Quit", width=8, command=root.quit)
btn_quit.place(x=510, y=100)

root.mainloop()

Step 1: Import the Libraries

The first line of the code is:

from pygame import mixer
from tkinter import *

Here we are importing mixer from the pygame library. Mixer is a module that can load and play music. Tkinter is Python’s built-in library to make GUI (Graphical User Interface). With Tkinter we can make windows, buttons, labels, etc.

So one library is for sound, and the other is for the window you see on the screen.

Step 2: Create the Window

root = Tk()
root.geometry("600x300")

root = Tk() makes the main window of the app. root.geometry("600x300") sets the size of the window to 600 pixels wide and 300 pixels tall. This is the area where all buttons and labels will appear.

Step 3: Initialize the Mixer and Load Music

mixer.init()
mixer.music.load("filename.mp3")

mixer.init() starts the mixer. After that, we load a music file. Here "filename.mp3" is written as the file name. You need to replace this with the actual song file you want to play. For example, if your song is named song.mp3, and it is in the same folder as your Python code, you should write:

mixer.music.load("song.mp3")

If the file is in another folder, you must give the full path. Example:

mixer.music.load("C:/Users/Om/Music/song.mp3")

So the filename is simply the music file you want to play. You can put the MP3 file in the same folder as your Python script to make things easier.

Step 4: Define Functions for Buttons

We want to control music with buttons like Play, Pause, Resume, and Quit. For that we create small functions.

def pause():
    mixer.music.pause()

def play():
    mixer.music.play()

def resume():
    mixer.music.unpause()
  • play() starts the music.
  • pause() stops the music temporarily.
  • resume() continues the paused music.

These functions will be connected to buttons later.

Step 5: Add Labels and Buttons

Now we want the user to see some text and click buttons.

Label(root, text="Welcome to music player", font="lucidia 30 bold").pack()

This shows a big label at the top with the text Welcome to music player.

Then we add buttons:

Button(text="Play", command=play).place(x=200, y=100)
Button(text="Pause", command=pause).place(x=250, y=100)
Button(text="Resume", command=resume).place(x=310, y=100)
Button(text="Quit", command=quit).place(x=380, y=100)
  • text="Play" is the button name.
  • command=play means when you click the button, the function play() runs.
  • .place(x=..., y=...) tells Tkinter where to put the button inside the window.

So four buttons are created: Play, Pause, Resume, and Quit. Quit will close the app.

Step 6: Run the Window

At the end we have:

root.mainloop()

This keeps the window running, so it does not close immediately. It listens for button clicks and other events. Without this line, the window would just appear and vanish.

How to Find filename.mp3

This is the most common beginner question. The "filename.mp3" is just the name of your music file. You need to have an MP3 file ready. Steps:

  1. Take any MP3 song or download one.
  2. Place it in the same folder where your Python file is saved.
    Example: If your Python file is musicplayer.py, keep your song in the same folder.
  3. Rename the file to something simple like song.mp3.
  4. Change the code to: mixer.music.load("song.mp3")

If you want to use a song from another folder, write the full path. Example:

mixer.music.load("D:/Music/my_song.mp3")

So, “filename.mp3” is not a special name. It is just a placeholder. Replace it with your actual music file name.

Final Thoughts

With only a few lines of Python, we made a working music player. This is the power of Python: you can build useful apps with very little code. Tkinter handled the user interface and buttons, while Pygame mixer handled the music.

For more such python projects visit here.

Post navigation

❮ Previous Post: How to Build Your own AI Model Step by Step | CodeHelping
Next Post: Artificial General Intelligence (AGI) Detailed Explanation | CodeHelping AI ❯

2 thoughts on “How to Build a Simple Music Player in Python | CodeHelping”

  1. Dovie Connelly says:
    August 20, 2025 at 3:02 pm

    Thank you for the auspicious writeup It in fact was a amusement account it Look advanced to more added agreeable from you By the way how could we communicate

    Reply
    1. Omkar Pathak says:
      August 21, 2025 at 7:50 pm

      Contact: basenect@gmail.com

      Reply

Leave a Reply Cancel reply

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

  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer

Copyright © 2025 codehelping.com.

Theme: Oceanly by ScriptsTown

Social Chat is free, download and try it now here!