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

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

from tkinter import *
from traceback import *
from win32com.client import Dispatch
import time,eyed3,threading

name = []

def openfile(index = [1]):
  global total,name

  filenames = filedialog.askopenfilenames(title = "音乐播放器",filetypes =[("mp3文件","*.mp3"),("WMA文件","*.wma"),("WAV文件","*.wav")])
  if filenames:
    for i in range(len(filenames)):
      media = wmp.newMedia(filenames[i])
      wmp.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]),使用wmp.currentMedia.name更好,在per函数中
  per_thread = threading.Thread(target = per)
  per_thread.daemnon = True
  wmp.controls.play()
  per_thread.start()
  #print(wmp.currentMedia.duration)#放到暂停那里居然可以用,而这里不行

def per():
  global total
  while wmp.playState !=1:
    progress_scal.set(int(wmp.controls.currentPosition))
    progress_scal.config(label = wmp.controls.currentPositionString)
    progress_scal.config(to = total,tickinterval = 50)
    time.sleep(1)
    root.title("%s" % wmp.currentMedia.name)

def stop():
  wmp.controls.stop()
def pause(event = None):
  wmp.controls.pause()

def uselist():
    pass
def fullscr():
    pass
def exitit():
  root.destroy()
def Previous_it():
  wmp.controls.previous()
def Next_it():
  wmp.controls.next()
def Volume_ctr(none):
  wmp.settings.Volume = vio_scale.get()
def Volume_add(i=[0]):
  wmp.settings.Volume =wmp.settings.Volume+5
  i.append(wmp.settings.Volume)
  vio_scale.set(wmp.settings.Volume)
def Volume_minus(i=[0]):
  wmp.settings.Volume = wmp.settings.Volume -5
  i.append(wmp.settings.Volume)
  vio_scale.set(wmp.settings.Volume)
def Scale_ctr(none):

  wmp.controls.currentPosition = var_scale.get()
  print(wmp.currentMedia.duration)
def Clear_list():
  wmp.currentPlaylist.clear()
  list_name.delete(1.0,END)
  name = []
  index = []
def List_random():
  wmp.settings.setMode("shuffle",True)
  play()
def List_loop():
  wmp.settings.setMode("loop",True)
  play()

root =Tk()
wmp = Dispatch("WMPlayer.OCX")

canvas = Canvas(root,width =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(row =0,column = 0,sticky = "nw",rowspan =2)

progress_lab = LabelFrame(root,text = "播放进度")
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 = "播放模式")
modee_lab.grid(row =4,column =0,rowspan =4,sticky = "ws")
var_mode = IntVar()
randomradio = Radiobutton(modee_lab,variable = var_mode,value = 1,text ="随机播放",command =List_random )
randomradio.grid(row =4,column =2)
inturnradio = Radiobutton(modee_lab,variable = var_mode,value =2,text= "顺序播放",command = play)
inturnradio.grid(row=4,column =3)
alloop = Radiobutton(modee_lab,variable = var_mode,value =2,text = "全部循环播放",command = List_loop)
alloop.grid(row =5,column = 2)
sinloop = Radiobutton(modee_lab,variable = var_mode,value =3,text = "单曲循环播放")
sinloop.grid(row =5,column =3)
previous_play = Button(modee_lab,text = "上一曲",height =1,command = Previous_it)
previous_play.grid(row =6,column =2,rowspan =2,pady =5)
next_play = Button(modee_lab,text = "下一曲",height =1,command = Next_it)
next_play.grid(row =6,column =3,rowspan =2,pady =5)

var_volume = IntVar()
vioce_lab = LabelFrame(root,text = "音量控制")
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 = "增加音量+",command =Volume_add)
vio_plus.grid(row =9,column =0,sticky = "w")
vio_minus = Button(vioce_lab,width =8,text ="减少音量-",command = Volume_minus)
vio_minus.grid(row =9,column =0,sticky ="e")

ctr_lab = LabelFrame(root,text = "播放控制",height =130)
ctr_lab.grid(row =0,column =1,rowspan =12,sticky = "ns")
btn_open = Button(ctr_lab,text ="打开音乐文件",width =10,command = openfile)
btn_open.grid(row=0,column =1)
btn_play = Button(ctr_lab,text ="播放",width =10,command = play)
btn_play.grid(row =1,column =1,pady =5)
btn_stop = Button(ctr_lab,text ="停止",width =10,command = stop)
btn_stop.grid(row =2,column =1,pady =5)
btn_pause = Button(ctr_lab,text ="暂停",width =10,command = pause)
btn_pause.grid(row =3,column =1,pady =5)

btn_playlist = Button(ctr_lab,text ="新建播放列表",width =10,command = uselist)
btn_playlist.grid(row =4,column =1,pady =5)

listimport = Button(ctr_lab,width =10,text = "导入列表")
listimport.grid(row =6,column =1,sticky ="nw",pady =5)
listexport = Button(ctr_lab,width =10,text = "导出列表")
listexport.grid(row =7,column =1,sticky = "nw",pady =5)
listdel_all = Button(ctr_lab,width =10,text = "清空列表",command = Clear_list)
listdel_all.grid(row =8,column =1,sticky ="nw",pady =5)
listdel_sel= Button(ctr_lab,width =10,text = "删除歌曲")
listdel_sel.grid(row =12,column =1,sticky = "nw",pady =5)
savelist_btn = Button(ctr_lab,text = "保存为列表")
savelist_btn.grid(row=9,column =1)
min_btn = Button(ctr_lab,text = "最小化窗口",command = root.iconify)
min_btn.grid(row =13,column =1)

time_lab= Label(root,width =20,height =2,text ="现在时间为:")
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)

root.mainloop()

以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。

(0)

相关推荐

  • python使用win32com库播放mp3文件的方法

    本文实例讲述了python使用win32com库播放mp3文件的方法.分享给大家供大家参考.具体实现方法如下: # Python supports COM, if you have the Win32 extensions # check your Python folder eg. D:\Python23\Lib\site-packages\win32com # also http://starship.python.net/crew/mhammond/win32/Downloads.html

  • 编写Python脚本来获取mp3文件tag信息的教程

    下面利用一个python的实例程序,来学习python.这个程序的目的就是分析出所有MP3文件的Tag信息并输出. import os # 导入os模块,提供文件路径,列出文件等方法 import sys # 导入sys模块,使用sys.modules获取模块中的所有内容,类似反射的功能 from UserDict import UserDict # 这个表示从UserDict类中导入UserDict,类似于Java中的 import UserDict.UserDict def stripnul

  • python使用wxPython打开并播放wav文件的方法

    本文实例讲述了python使用wxPython打开并播放wav文件的方法.分享给大家供大家参考.具体实现方法如下: ''' wx_lib_filebrowsebutton_sound.py select a sound file and play it wx.lib.filebrowsebutton.FileBrowseButton(parent, labelText, fileMask) (combines wx.TextCtrl and wxFileDialog widgets) wx.So

  • Python修改MP3文件的方法

    本文实例讲述了Python修改MP3文件的方法.分享给大家供大家参考.具体如下: 用这个程序修改后的MP3比原来要小一些了,因为一张图片被删除了,起到了给MP3"瘦身"的作用.在一些mp3中,每个都有一张400多K的图片,10几个MP3,就相当一个普通MP3文件的大小了. # -*- coding: cp936 -*- """ 将MP3文件中的ID3V2.3部分去掉,以便在MP3机上播放 用法:mp3lcear [源mp3目录] [生成的mp3目录] &q

  • Python基于pygame模块播放MP3的方法示例

    本文实例讲述了Python基于pygame模块播放MP3的方法.分享给大家供大家参考,具体如下: 安装pygame(可参考:安装Python和pygame及相应的环境变量配置) pip安装这个whl文件 装完就直接跑代码啦,很短的 import time import pygame file=r'C:\Users\chan\Desktop\Adele - All I Ask.mp3' pygame.mixer.init() print("播放音乐1") track = pygame.m

  • python获取mp3文件信息的方法

    本文实例讲述了python获取mp3文件信息的方法.分享给大家供大家参考.具体如下: 将代码生成.py文件放在目录下运行,可以获取该目录的所有mp3文件的信息,需要使用ID3库 import os, sys from ID3 import * files = os.listdir(os.getcwd()) for f in files: x = os.path.splitext(f) if x[1] == '.mp3': n = x[0].split(' - ') author = n[0] t

  • python实现定时播放mp3

    程序很简单,主要是 mp3play 模块的应用 import mp3play, time filename = "Should It Matter.mp3" clip = mp3play.load(filename) while 1: if time.localtime().tm_min % 30 == 0: clip.play() print "\nStart to play" time.sleep(clip.seconds()) clip.stop() prin

  • Python调用系统底层API播放wav文件的方法

    本文实例讲述了Python调用系统底层API播放wav文件的方法.分享给大家供大家参考,具体如下: 这里未使用其他库,只是使用 pywin32 调用系统底层 API 播放 wav 文件. 具体代码如下: # Our raison d'etre - playing sounds import pywintypes import struct import win32event import win32com.directsound.directsound as ds import os WAV_H

  • python使用beautifulsoup从爱奇艺网抓取视频播放

    复制代码 代码如下: import sysimport urllibfrom urllib import requestimport osfrom bs4 import BeautifulSoup class DramaItem:    def __init__(self, num, title, url):        self.num = num        self.title = title        self.url = url    def __str__(self):   

  • Python写入数据到MP3文件中的方法

    本文实例讲述了Python写入数据到MP3文件中的方法.分享给大家供大家参考.具体分析如下: 通过Mp3的Id3V1数据段的数据来修正Mp3文件的正确名字,但是,有时候这个数据断中的数据是空的,所以这里写一个修改Id3V1数据段的数据的函数,同样是练习. 使用方法: writeMp3Header[ SongName] = '测试歌曲名称' writeMp3Header[ SongPeople] = '不得闲' writeMp3Header[ ZhuanJi] = '专辑' writeMp3Hea

  • python使用PyGame播放Midi和Mp3文件的方法

    本文实例讲述了python使用PyGame播放Midi和Mp3文件的方法.分享给大家供大家参考.具体实现方法如下: ''' pg_midi_sound101.py play midi music files (also mp3 files) using pygame tested with Python273/331 and pygame192 by vegaseat ''' import pygame as pg def play_music(music_file): ''' stream m

  • Python编程修改MP3文件名称的方法

    本文实例讲述了Python编程修改MP3文件名称的方法.分享给大家供大家参考,具体如下: 最近刚刚开始学习Python,顺便锻炼思维写的一个小函数,专门用来修改那些网上下载的mp3歌曲后名称不正确时候,使用该函数,使用方法 ModifyMp3FileInfo(r'E:/音乐/12345.mp3') Python代码: #!修改下载的Mp3文件名称为正确的Mp3文件 def ModifyMp3FileInfo(filename): mp3Id3V1 = { "tag":{"va

随机推荐