SoFunction
Updated on 2024-10-29

python3 music player simple implementation code

This article example shares the key code of python3 music player for your reference, the details are as follows

from tkinter import *
from traceback import *
from  import Dispatch
import time,eyed3,threading
 
name = []
 
def openfile(index = [1]):
 global total,name
  
 filenames = (title = "Music Player",filetypes =[("mp3 file","*.mp3"),("WMA files","*.wma"),("WAV file","*.wav")])
 if filenames:
  for i in range(len(filenames)):
   media = (filenames[i])
   (media)
   
   print(filenames[i])
   
   coco = (filenames[i])#eyed3 module reads mp3 information
   total = int(.time_secs)
   minute = int(.time_secs)//60
   sec = int(.time_secs)%60
   length = int(.time_secs)
   
   name = filenames[i].split("/")
   
   i =index[-1]
   list_name.insert(END,str(i)+"."+name[-1])
   list_name.insert(END," "*6)
   if sec >=10:
    list_name.insert(END,"0%d:%d" %(minute,sec)+ "\n")
   else:
    list_name.insert(END,"0%s:0%d" %(minute,sec)+ "\n")
   i = i +1
   (i)
def play(event = None):
 #("%s" % name[-1]), use better, in per function
 per_thread = (target = per)
 per_thread.daemnon = True
 ()
 per_thread.start()
 #print()# works on pause, but not here.
 
  
def per():
 global total
 while  !=1:
  progress_scal.set(int())
  progress_scal.config(label = )
  progress_scal.config(to = total,tickinterval = 50)
  (1)
  ("%s" % )
  
def stop():
 ()
def pause(event = None):
 ()
 
def uselist():
  pass
def fullscr():
  pass
def exitit():
 ()
def Previous_it():
 ()
def Next_it():
 ()
def Volume_ctr(none):
  = vio_scale.get()
def Volume_add(i=[0]):
  =+5
 ()
 vio_scale.set()
def Volume_minus(i=[0]):
  =  -5
 ()
 vio_scale.set()
def Scale_ctr(none):
 
  = var_scale.get()
 print()
def Clear_list():
 ()
 list_name.delete(1.0,END)
 name = []
 index = []
def List_random():
 ("shuffle",True)
 play()
def List_loop():
 ("loop",True)
 play()
  
root =Tk()
wmp = Dispatch("")
 
canvas = Canvas(root,width =150,height = 100,bg = "blue")
filename = PhotoImage(file = "")
image =canvas.create_image((0,0),image = filename)
(x=0,y=0)
(image,79,50)
(row =0,column = 0,sticky = "nw",rowspan =2)
 
 
progress_lab = LabelFrame(root,text = "Playback progress")
progress_lab.grid(row =2,column =0,sticky = "we",rowspan = 2)
var_scale = DoubleVar()
progress_scal = Scale(progress_lab,orient = HORIZONTAL,showvalue = 0,length =180,variable = var_scale)
#progress_scal.bind("<Button-1>",pause)
#progress_scal.bind("")
#progress_scal.bind("<ButtonRelease-1>",play)
progress_scal.grid(row =3,column =0)
 
modee_lab = LabelFrame(root,text = "Playback Mode")
modee_lab.grid(row =4,column =0,rowspan =4,sticky = "ws")
var_mode = IntVar()
randomradio = Radiobutton(modee_lab,variable = var_mode,value = 1,text ="Randomized playback.",command =List_random )
(row =4,column =2)
inturnradio = Radiobutton(modee_lab,variable = var_mode,value =2,text= "Sequential playback",command = play)
(row=4,column =3)
alloop = Radiobutton(modee_lab,variable = var_mode,value =2,text = "All on a loop.",command = List_loop)
(row =5,column = 2)
sinloop = Radiobutton(modee_lab,variable = var_mode,value =3,text = "Single song loop playback.")
(row =5,column =3)
previous_play = Button(modee_lab,text = "Previous song.",height =1,command = Previous_it)
previous_play.grid(row =6,column =2,rowspan =2,pady =5)
next_play = Button(modee_lab,text = "Next song.",height =1,command = Next_it)
next_play.grid(row =6,column =3,rowspan =2,pady =5)
 
var_volume = IntVar()
vioce_lab = LabelFrame(root,text = "Volume Control")
vioce_lab.grid(row =8,column =0,sticky = "wes")
vio_scale = Scale(vioce_lab,orient = HORIZONTAL,length =170,variable = var_volume,command =Volume_ctr)
vio_scale.set(30)
vio_scale.grid(row =8,column =0)
vio_plus = Button(vioce_lab,width =8,text = "Increase volume +",command =Volume_add)
vio_plus.grid(row =9,column =0,sticky = "w")
vio_minus = Button(vioce_lab,width =8,text ="Reduce volume-",command = Volume_minus)
vio_minus.grid(row =9,column =0,sticky ="e")
 
ctr_lab = LabelFrame(root,text = "Playback control",height =130)
ctr_lab.grid(row =0,column =1,rowspan =12,sticky = "ns")
btn_open = Button(ctr_lab,text ="Open music file.",width =10,command = openfile)
btn_open.grid(row=0,column =1)
btn_play = Button(ctr_lab,text ="Play",width =10,command = play)
btn_play.grid(row =1,column =1,pady =5)
btn_stop = Button(ctr_lab,text ="Stop.",width =10,command = stop)
btn_stop.grid(row =2,column =1,pady =5)
btn_pause = Button(ctr_lab,text ="Pause.",width =10,command = pause)
btn_pause.grid(row =3,column =1,pady =5)
 
btn_playlist = Button(ctr_lab,text ="New Playlist",width =10,command = uselist)
btn_playlist.grid(row =4,column =1,pady =5)
 
 
listimport = Button(ctr_lab,width =10,text = "Import List")
(row =6,column =1,sticky ="nw",pady =5)
listexport = Button(ctr_lab,width =10,text = "Export List")
(row =7,column =1,sticky = "nw",pady =5)
listdel_all = Button(ctr_lab,width =10,text = "Empty the list.",command = Clear_list)
listdel_all.grid(row =8,column =1,sticky ="nw",pady =5)
listdel_sel= Button(ctr_lab,width =10,text = "Delete the song.")
listdel_sel.grid(row =12,column =1,sticky = "nw",pady =5)
savelist_btn = Button(ctr_lab,text = "Save as list")
savelist_btn.grid(row=9,column =1)
min_btn = Button(ctr_lab,text = "Minimize the window.",command = )
min_btn.grid(row =13,column =1)
 
time_lab= Label(root,width =20,height =2,text ="The time is now:")
time_lab.grid(row =12,column =0,sticky = "nw",pady =5)
time_text= Text(root,width =30,height =3,foreground ="green")
time_text.grid(row =10,column =0,sticky = "nw",pady =5)
 
list_name = Text(root,height =18,width =110)
list_name.grid(row =0,column =2,sticky = "n",rowspan =6)
 
 
()

This is the whole content of this article, I hope to help you learn python programming.