Don't Have A Music Player Lets Create One....



For Music Player you need two libraries pygame and tkinter.

You need to install both via python -m install 'library name' command.

You can get all the libraries from here: https://pypi.org/project/pip/

Now lets dive into code:-

  1. Step 1 would be create a function for playing the song.
  2. Next to pause the song
  3. Then finally to resume the song
  4. And finally to stop the song
  5. So there is something called as ListBox which returns you a playlist env
  6. You just need to get the song in folder and via os iterate throught the folder
  7. Insert the song files to the playlist
  8. Now final step would be create different buttons for different functions
  9. And play around with the inbuilt methods in the mixer lib which comes from pygame
  10. Lets view the code now.

from pygame import mixer
from tkinter import *
import os


def play_song():
current_song = playlist.get(ACTIVE)
print(current_song)
mixer.music.load(current_song)
song_status.set("Playing")
mixer.music.play()


def pause_song():
song_status.set("Paused")
mixer.music.pause()


def stop_song():
song_status.set("Stopped")
mixer.music.stop()


def resume_song():
song_status.set("Resuming")
mixer.music.unpause()


root = Tk()
root.title('UJ Music Player')

mixer.init()
song_status = StringVar()
song_status.set("choosing")

playlist = Listbox(root, selectmode=SINGLE, bg="Pink", fg="black", font=('arial', 15), width=40)
playlist.grid(columnspan=5)

os.chdir(r'D:\songs')
songs = os.listdir()
for s in songs:
playlist.insert(END, s)

play_btn = Button(root, text="play", command=play_song)
play_btn.config(font=('arial', 20), bg="Green", fg="white", padx=7, pady=7)
play_btn.grid(row=1, column=0)

pause_btn = Button(root, text="Pause", command=pause_song)
pause_btn.config(font=('arial', 20), bg="Blue", fg="white", padx=7, pady=7)
pause_btn.grid(row=1, column=1)

stop_btn = Button(root, text="Stop", command=stop_song)
stop_btn.config(font=('arial', 20), bg="Red", fg="white", padx=7, pady=7)
stop_btn.grid(row=1, column=2)

Resume_btn = Button(root, text="Resume", command=resume_song)
Resume_btn.config(font=('arial', 20), bg="Orange", fg="white", padx=7, pady=7)
Resume_btn.grid(row=1, column=3)

mainloop()
    



Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)