python3音乐播放器简单实现代码

家电维修 2023-07-16 19:17www.caominkang.com家电维修技术

本文实例为大家讲解了python3音乐播放器的关键代码,供大家参考,具体内容如下

from tkinter import 
from traceback import 
from in32.client import Dispatch
import time,eyed3,threading
 
name = []
 
def openfile(index = [1]):
 global total,name
  
 filenames = filedialog.askopenfilenames(title = "音乐播放器",filetypes =[("mp3文件",".mp3"),("WMA文件",".ma"),("WAV文件",".av")])
 if filenames:
  for i in range(len(filenames)):
   media = mp.neMedia(filenames[i])
   mp.currentPlaylist.appendItem(media)
   
   print(filenames[i])
   
   coco = eyed3.load(filenames[i])#eyed3模块读取mp3信息
   total = int(coco.info.time_secs)
   minute = int(coco.info.time_secs)//60
   sec = int(coco.info.time_secs)%60
   length = int(coco.info.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
   index.append(i)
def play(event = None):
 #root.title("%s" % name[-1]),使用mp.currentMedia.name更好,在per函数中
 per_thread = threading.Thread(target = per)
 per_thread.daemnon = True
 mp.controls.play()
 per_thread.start()
 #print(mp.currentMedia.duration)#放到暂停那里居然可以用,而这里不行
 
  
def per():
 global total
 hile mp.playState !=1:
  progress_scal.set(int(mp.controls.currentPosition))
  progress_scal.config(label = mp.controls.currentPositionString)
  progress_scal.config(to = total,tickinterval = 50)
  time.sleep(1)
  root.title("%s" % mp.currentMedia.name)
  
def s():
 mp.controls.s()
def pause(event = None):
 mp.controls.pause()
 
def uselist():
  pass
def fullscr():
  pass
def exitit():
 root.destroy()
def Previous_it():
 mp.controls.previous()
def Next_it():
 mp.controls.next()
def Volume_ctr(none):
 mp.settings.Volume = vio_scale.get()
def Volume_add(i=[0]):
 mp.settings.Volume =mp.settings.Volume+5
 i.append(mp.settings.Volume)
 vio_scale.set(mp.settings.Volume)
def Volume_minus(i=[0]):
 mp.settings.Volume = mp.settings.Volume -5
 i.append(mp.settings.Volume)
 vio_scale.set(mp.settings.Volume)
def Scale_ctr(none):
 
 mp.controls.currentPosition = var_scale.get()
 print(mp.currentMedia.duration)
def Clear_list():
 mp.currentPlaylist.clear()
 list_name.delete(1.0,END)
 name = []
 index = []
def List_random():
 mp.settings.setMode("shuffle",True)
 play()
def List_loop():
 mp.settings.setMode("loop",True)
 play()
  
root =Tk()
mp = Dispatch("WMPlayer.OCX")
 
canvas = Canvas(root,idth =150,height = 100,bg = "blue")
filename = PhotoImage(file = "girl.gif")
image =canvas.create_image((0,0),image = filename)
canvas.place(x=0,y=0)
canvas.coords(image,79,50)
canvas.grid(ro =0,column = 0,sticky = "n",rospan =2)
 
 
progress_lab = Labelframe(root,text = "播放进度")
progress_lab.grid(ro =2,column =0,sticky = "e",rospan = 2)
var_scale = DoubleVar()
progress_scal = Scale(progress_lab,orient = HORIZONTAL,shovalue = 0,length =180,variable = var_scale)
#progress_scal.bind("",pause)
#progress_scal.bind("")
#progress_scal.bind("",play)
progress_scal.grid(ro =3,column =0)
 
modee_lab = Labelframe(root,text = "播放模式")
modee_lab.grid(ro =4,column =0,rospan =4,sticky = "s")
var_mode = IntVar()
randomradio = Radiobutton(modee_lab,variable = var_mode,value = 1,text ="随机播放",mand =List_random )
randomradio.grid(ro =4,column =2)
inturnradio = Radiobutton(modee_lab,variable = var_mode,value =2,text= "顺序播放",mand = play)
inturnradio.grid(ro=4,column =3)
alloop = Radiobutton(modee_lab,variable = var_mode,value =2,text = "全部循环播放",mand = List_loop)
alloop.grid(ro =5,column = 2)
sinloop = Radiobutton(modee_lab,variable = var_mode,value =3,text = "单曲循环播放")
sinloop.grid(ro =5,column =3)
previous_play = Button(modee_lab,text = "上一曲",height =1,mand = Previous_it)
previous_play.grid(ro =6,column =2,rospan =2,pady =5)
next_play = Button(modee_lab,text = "下一曲",height =1,mand = Next_it)
next_play.grid(ro =6,column =3,rospan =2,pady =5)
 
var_volume = IntVar()
vioce_lab = Labelframe(root,text = "音量控制")
vioce_lab.grid(ro =8,column =0,sticky = "es")
vio_scale = Scale(vioce_lab,orient = HORIZONTAL,length =170,variable = var_volume,mand =Volume_ctr)
vio_scale.set(30)
vio_scale.grid(ro =8,column =0)
vio_plus = Button(vioce_lab,idth =8,text = "增加音量+",mand =Volume_add)
vio_plus.grid(ro =9,column =0,sticky = "")
vio_minus = Button(vioce_lab,idth =8,text ="减少音量-",mand = Volume_minus)
vio_minus.grid(ro =9,column =0,sticky ="e")
 
ctr_lab = Labelframe(root,text = "播放控制",height =130)
ctr_lab.grid(ro =0,column =1,rospan =12,sticky = "ns")
btn_open = Button(ctr_lab,text ="打开音乐文件",idth =10,mand = openfile)
btn_open.grid(ro=0,column =1)
btn_play = Button(ctr_lab,text ="播放",idth =10,mand = play)
btn_play.grid(ro =1,column =1,pady =5)
btn_s = Button(ctr_lab,text ="停止",idth =10,mand = s)
btn_s.grid(ro =2,column =1,pady =5)
btn_pause = Button(ctr_lab,text ="暂停",idth =10,mand = pause)
btn_pause.grid(ro =3,column =1,pady =5)
 
btn_playlist = Button(ctr_lab,text ="新建播放列表",idth =10,mand = uselist)
btn_playlist.grid(ro =4,column =1,pady =5)
 
 
listimport = Button(ctr_lab,idth =10,text = "导入列表")
listimport.grid(ro =6,column =1,sticky ="n",pady =5)
listexport = Button(ctr_lab,idth =10,text = "导出列表")
listexport.grid(ro =7,column =1,sticky = "n",pady =5)
listdel_all = Button(ctr_lab,idth =10,text = "清空列表",mand = Clear_list)
listdel_all.grid(ro =8,column =1,sticky ="n",pady =5)
listdel_sel= Button(ctr_lab,idth =10,text = "删除歌曲")
listdel_sel.grid(ro =12,column =1,sticky = "n",pady =5)
savelist_btn = Button(ctr_lab,text = "保存为列表")
savelist_btn.grid(ro=9,column =1)
min_btn = Button(ctr_lab,text = "最小化窗口",mand = root.iconify)
min_btn.grid(ro =13,column =1)
 
time_lab= Label(root,idth =20,height =2,text ="现在时间为:")
time_lab.grid(ro =12,column =0,sticky = "n",pady =5)
time_text= Text(root,idth =30,height =3,foreground ="green")
time_text.grid(ro =10,column =0,sticky = "n",pady =5)
 
list_name = Text(root,height =18,idth =110)
list_name.grid(ro =0,column =2,sticky = "n",rospan =6)
 
 
root.mainloop()

Copyright © 2016-2025 www.jianfeikang.com 建飞家电维修 版权所有 Power by