Simple Music Player Using Python

Simple Music Player Using Python

Hello friends, do you know that there are many ways to make music players from simple to advanced level.

Here, we are presenting you with two very very basic music player in python.

One of them is a simple program and the other one is the GUI program.

For the simple program, we have used pygame which is a famous gaming module in python and its use is very simple.

So, you need to install pygame inside your system if you don’t have it.

To install pygame type "pip install pygame" inside cmd.

For the GUI one, you should have tkinter and pygame both.

There are so many small tkinter projects which you can see if you have any problem with tkinter.

So, now let’s see the codes for both programs.

Code for the GUI program

from pygame import mixer
from tkinter import *

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

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


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


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


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


Label(root, text="Welcome to music player", font="lucidia 30 bold").pack()
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)

root.mainloop()

Code for simple program


import pygame
from pygame import mixer

pygame.init()
mixer.init()
screen = pygame.display.set_mode((600, 400))
mixer.music.load("filename.mp3")
mixer.music.play()

print("Press 'p' to pause 'r' to resume")
print("Press 'q' to quit")
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                mixer.music.pause()
            if event.key == pygame.K_r:
                mixer.music.unpause()
            if event.key == pygame.K_q:
                running = False
quit()

You just need to copy and paste it into your IDE and just run it. And also don’t forget that your music file with the name ‘filename’ should be at the same place where your program exists i.e. they both should be inside the same directory

Thanks for reading

If you have any query then feel free to comment

Keep with us as we post much content like this


Also read:

Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you

4 thoughts on “Simple Music Player Using Python

  1. Traceback (most recent call last):
    File “C:\Users\Korisnik\Desktop\Mp3 player.py”, line 8, in
    mixer.music.load(“filename.mp3”)
    pygame.error: Couldn’t open ‘filename.mp3’

    1. the filename is the song you want to play, replace this with your original song name with the instructions given above

Comments are closed.