Autoplay next song & Working forward button|Make a Working Music Player using Python|MP3 player GUIMusic player using tkinter|Pydroid3|Part 5
Important Note:-The following code still consists some errors like once it starts playing a music then no other button is clickable.We will solve this problem in upcoming tutorials.
If You have watched Part 4:-
- Then Change the variable name which are in red in the following code.
- Give command to forward button by using command=fwd() in the button widget.
- The updated part of the code is(In the playsong() function):-name=f'/storage/emulated/0/NCS/{Present_song}'
pygame.mixer.init()
pygame.mixer.music.load(name)
pygame.mixer.music.play()
SONG_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(SONG_END)
while True:
for event in pygame.event.get():
if event.type == SONG_END:
forward()
root.update()
#Autplay Next Song
#Forward
def forward():
for songindex in added_songs.curselection():
nextsongindex=songindex+1
added_songs.activate(nextsongindex)
play_song()
Full CODE:-
from playsound import playsound
from tkinter import *
import tkinter as tk
from tkinter import filedialog
import pygame
root=tk.Tk()
root.geometry("1020x2180")
root.title("Music Player")
root.config(bg='white')
frame1=tk.Frame(root,bg='white')
frame1.pack()
frame2=tk.Frame(root,bg='white')
frame2.pack()
frame3=tk.Frame(root,bg='white')
frame3.pack()
frame4=tk.Frame(root,bg='white')
frame4.pack(pady=40)
frame5=tk.Frame(root,bg='#FF8BF0',borderwidth=5)
frame5.pack(pady=20)
#Playlist
#ADDING A SCROLLBAR
Title=Label(frame5,text="Your Playlist",width=35,bg='white',borderwidth=5)
Title.pack(padx=20,pady=20)
myscrollbar=Scrollbar(frame5,orient="vertical")
myscrollbar.pack(side="right",fill="y")
added_songs=Listbox(frame5,borderwidth=5,width=35)
added_songs.pack()
#Function to Add songs
def add_songs():
songs=filedialog.askopenfilename(initialdir='/storage/emulated/0/',filetypes=(('mp3 files','*.mp3'),))
songs=songs.replace('/storage/emulated/0/NCS/','')
added_songs.insert(END,songs)
#TOPBAR
Top_btn=Button(frame1,text="Add Songs",relief="raised",borderwidth=5,width=50,command=add_songs)
Top_btn.pack(expand=YES,fill=X,padx=20,pady=20)
#Timebar
time_label=Label(frame3,text="0:00")
time_label.pack(padx=20,pady=10)
#Center Image
Centerimage=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_134042.png')
#Center image placement
Centerimgbutton=Label(frame2,image=Centerimage,borderwidth=2,bg='white')
Centerimgbutton.grid(padx=50,pady=130)
#BUTTON IMAGES
img_play_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_131217.png')
img_stop_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_195345.png')
img_fwd_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_130319.png')
img_back_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_130655.png')
img_shuffle_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_131516.png')
img_loop_btn=PhotoImage(file='/storage/emulated/0/pixelLab/20211111_131551.png')
#Playing Songs
def play_song():
global added_songs
Present_song=added_songs.get(ACTIVE)
name=f'/storage/emulated/0/NCS/{Present_song}'
pygame.mixer.init()
pygame.mixer.music.load(name)
pygame.mixer.music.play()
SONG_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(SONG_END)
while True:
for event in pygame.event.get():
if event.type == SONG_END:
forward()
root.update()
#Autplay Next Song
#Forward
def forward():
for songindex in added_songs.curselection():
nextsongindex=songindex+1
added_songs.activate(nextsongindex)
play_song()
# pass
#FUNCTION FOR CHANGING THE BUTTON
def change_pic(variable):
if variable == 1:
play_btn.configure(image=img_stop_btn)
play_btn.configure(command=lambda variable = 0:change_pic(variable))
root.update()
play_song()
elif variable ==0:
play_btn.configure(image=img_play_btn)
play_btn.configure(command=lambda variable = 1:change_pic(variable))
root.update()
#BUTTONS
shuffle_btn=Button(frame4,image=img_shuffle_btn,bg='white')
play_btn=Button(frame4,bg='white',image=img_play_btn,borderwidth=0,command=lambda variable=1 :change_pic(1))
stop_btn=Button(frame4,image=img_stop_btn,borderwidth=0,command=lambda variable=0 :change_pic(0))
fwd_btn=Button(frame4,bg='white',image=img_fwd_btn,borderwidth=0,command=forward)
back_btn=Button(frame4,bg='white',image=img_back_btn,borderwidth=0)
loop_btn=Button(frame4,bg='white',image=img_loop_btn,borderwidth=0)
shuffle_btn.pack(side=LEFT,padx=70,pady=10)
back_btn.pack(side=LEFT,padx=5,pady=10)
play_btn.pack(side=LEFT,padx=5,pady=10)
fwd_btn.pack(side=LEFT,padx=5,pady=10)
loop_btn.pack(side=LEFT,padx=70,pady=10)
root.mainloop()
#Code ended