Python-音樂播放器

自己在學python的過程中寫的音樂播放器,基本上完成了讀取,播放,暫停,圖片的功能,後期就不再加了,不說了,上圖片:
前端:
在這裏插入圖片描述
後臺:
在這裏插入圖片描述
運行:
在這裏插入圖片描述
還可以自己編曲,不過編曲這一部分就是隨機編曲,而沒有用到神經網絡之類的
在這裏插入圖片描述
上代碼:

#utf-8
import tkinter
from PIL import Image,ImageTk
import pygame
import time
import os
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
from tkinter import ttk
import eyed3
import librosa
import random
import 旋律生成 as cmusic

top=tkinter.Tk()#窗口
top.geometry('800x400')#窗口大小
top.title('【書生君陌】音樂播放器')#窗口名稱
# loop1=1#循環定義
# stime=1#時間

def screen(t):#透明度設置
    t=int(t)
    top.attributes('-alpha',t/100)#設置透明度

width=top.winfo_screenwidth()#寬度
height=top.winfo_screenheight()-100#高度

pygame.init()#窗口初始化
file=tkinter.StringVar()#獲得文件列表
files=tkinter.StringVar()#獲得文件列表
filename=tkinter.StringVar()
filenames=[]
v=tkinter.StringVar()#獲得文件列表
v1=tkinter.StringVar()#獲得文件列表
var_mode = tkinter.IntVar()#播放順序
var_mode2 = tkinter.IntVar()#播放狀態
var_mode3 = tkinter.IntVar()#播放進度
v2=tkinter.DoubleVar()#播放進度
longt=1

def search():#搜索文件
    # file_=askopenfilename()
    file_=askdirectory()
    return file_

def voice(t):#音量
    t=int(t)
    pygame.mixer.music.set_volume(t/100)#設置音量

def showtime():#顯示時間
    c=time.strftime("%H:%M:%S")
    stime.configure(text=c)

    num=pygame.mixer.music.get_pos()/1000
    print('jindu',num,longt,float(num/longt))
    w3.set(int(num/longt*100))
    print(w3.get())

    top.after(1000,showtime)

def musicplay():
    global  filename,filenames
    f=search()#選擇文件夾
    # print(f)
    filename=f
    file.set(f)
    f=os.listdir(f)
    filenames=[]
    for fname in f:
        fp=os.path.join(fname)
        filenames.append(fp)
    c2['value'] = filenames
    c2.current(0)
    # lbTime = tkinter.Label(top, anchor='w')
    # lbTime.place(x=25, y=150)

def start():
    # print(filename+c2.get())
    global longt
    var_mode2.set(1)
    print(filename+'/'+c2.get())
    pygame.mixer.music.load(filename+'/'+c2.get())
    # longt=int(librosa.get_duration(filename=filename+'/'+c2.get()))
    if c2.get()[-4:]=='.mp3':
        longt=eyed3.load(filename+'/'+c2.get())
        longt=int(longt.info.time_secs)
    else:
        longt=39
    print(longt)
    print(var_mode.get())
    pygame.mixer.music.play()
    # w3.set(0)
    # pygame.mixer.music.play()  # 停止播放

def stop():
    var_mode2.set(0)
    pygame.mixer.music.stop()  # 停止播放

def pause():
    var_mode2.set(0)
    pygame.mixer.music.pause()  # 暫停

def unpause():
    var_mode2.set(1)
    pygame.mixer.music.unpause()  # 繼續播放

def picture():  # 保存的路徑不能有中文,若需要中文則吧/換成\
    path_s = askopenfilename()
    files.set(path_s)
    img_open = Image.open(e1.get())
    img = ImageTk.PhotoImage(img_open)
    l1.config(image=img)
    l1.image = img

# def create():
#     top = tkinter.Toplevel()
#     top.title('使用提示')
#     top.geometry("400x400")
#     t = "關於照片,新建一個存放圖片的文件,用英文命名,然後存裏面的圖片也用英文命名。關於音樂: 新建一個名字叫音樂的文件,把歌曲添加到該文件夾。"
#     msg = tkinter.Message(top, text=t)
#     msg.config(font=('times', 24, 'italic'))
#     msg.place(x=0, y=0)

def gettime():
    t = time.strftime('%H%M%S')
    s = int(t[0:2])
    d = int(t[2:4])
    f = int(t[4:6])
    g = s * 60 * 60 + d * 60 + f
    return g

def Listloop():
    global var_mode2
    print(pygame.mixer.music.get_busy(),var_mode.get())
    if var_mode.get()==1and pygame.mixer.music.get_busy()==False and var_mode2.get()==1:
        start()
    elif var_mode.get()==2 and pygame.mixer.music.get_busy()==False and var_mode2.get()==1:
        stop()
    elif var_mode.get()==3 and pygame.mixer.music.get_busy()==False and var_mode2.get()==1:
        print(c2.get())
        print(filenames)
        num=filenames.index(c2.get())
        if num<len(filenames)-1:
            c2.current(num+1)
        else:
            c2.current(0)
        print(c2.get())
        start()
    elif var_mode.get()==4 and pygame.mixer.music.get_busy()==False and var_mode2.get()==1:
        print(c2.get())
        print(filenames)
        num=filenames.index(c2.get())
        if num<len(filenames)-1:
            c2.current(num+1)
            print(c2.get())
            start()
        else:
            stop()
    elif var_mode.get()==5 and pygame.mixer.music.get_busy()==False and var_mode2.get()==1:
        print(c2.get())
        print(filenames)
        num=random.randint(0,len(filenames))
        c2.current(num)
        print(num,c2.get())
        start()
    # elif var_mode3.get()==0:
    #     var_mode3.set(1)
    top.after(3000,Listloop)

def create():
    cmusic.creatmusic(filename+'/'+e1.get())
    f=os.listdir(filename)
    filenames=[]
    for fname in f:
        fp=os.path.join(fname)
        filenames.append(fp)
    c2['value'] = filenames

# def lenth(v2):
#     global var_mode2,var_mode3
#     var_mode2.set(1)
#     print(v2)
#     var_mode3.set(int(v2) / 100)
#     print(var_mode3.get())
    # pygame.mixer.music.play(0,num*longt)
    # print('hi',pygame.mixer.music.get_busy(),var_mode3.get())

# def jindu():
#     num=pygame.mixer.music.get_pos()
#     print('jindu',num)
#     w3.set(num/longt)
#     print(w3.get())
#     top.after(1000,jindu)

if __name__=="__main__":
    errmsg = 'Error!'
    img_open = Image.open('封面.jpg')
    img = ImageTk.PhotoImage(img_open)
    pygame.mixer.music.set_volume(1)#設置音量
    top.attributes('-alpha',1)#設置透明度
    Listloop()
    var_mode2.set(0)
    # var_mode3.set(1)
    # 選擇文件
    tkinter.Button(top, text="選擇文件夾", command=musicplay, width=10, bg="sky blue").place(x=20, y=20)
    tkinter.Entry(top, text=file, width=25, state='readonly').place(x=120, y=20)

    c2 = ttk.Combobox(top,width=22)
    c2.pack()
    c2.place(x=120,y=90)
    # 選擇圖片
    tkinter.Button(top, text='選擇圖片', command=picture, width=10, bg="sky blue").place(x=20, y=55)
    e1 = tkinter.Entry(top, text=files, state='readonly', width=25)
    e1.place(x=120, y=55)
    l1 = tkinter.Label(top)  # 圖片放置位置
    l1.place(x=320, y=0)
    l1.config(image=img)
    l1.image = img
    #播放設置
    var_mode.set(1)
    sinloop = tkinter.Radiobutton(top, variable=var_mode, value=1, text="單曲循環")
    sinloop.place(x=20,y=100)
    single = tkinter.Radiobutton(top, variable=var_mode, value=2, text="單曲播放")
    single.place(x=20,y=120)
    allloop = tkinter.Radiobutton(top, variable=var_mode, value=3, text="列表循環")
    allloop.place(x=20,y=140)
    allsin = tkinter.Radiobutton(top, variable=var_mode, value=4, text="順序播放")
    allsin.place(x=20,y=160)
    ranloop = tkinter.Radiobutton(top, variable=var_mode, value=5, text="隨機播放")
    ranloop.place(x=20,y=180)
    # 開始,暫停,繼續播放,結束播放
    tkinter.Button(top, text="開始播放", command=start, width=7, bg="sky blue").place(x=100, y=225)
    tkinter.Button(top, text="暫停播放", command=pause, width=7, bg="sky blue").place(x=170, y=245)
    tkinter.Button(top, text="繼續播放", command=unpause, width=7, bg="sky blue").place(x=170, y=205)
    tkinter.Button(top, text="結束播放", command=stop, width=7, bg="sky blue").place(x=240, y=225)
    # 自己生成旋律
    e2=tkinter.Entry(top,text='編曲名稱', width=25)
    e2.place(x=120,y=130)
    e2.insert(0,'中國風')
    tkinter.Button(top, text="隨機編曲", command=create, width=7, bg="sky blue").place(x=170, y=165)

    # 音量
    v.set('100')
    w1 = tkinter.Scale(top, from_=0, to=100, orient="horizontal", length=100, variable=v, command=voice,label="音量")
    w1.place(x=20, y=290)
    w1.set(100)
    #透明度
    v1.set('100')
    w2 = tkinter.Scale(top, from_=30, to=100, orient="horizontal", length=100, variable=v1, command=screen,label="透明度")
    w2.place(x=180, y=290)
    w2.set(100)
    # 播放進度
    v2.set(0.0)
    w3 = tkinter.Scale(top, from_=0, to=100, orient="horizontal", width=10, length=475, variable=v2)
    w3.place(x=320, y=360)
    w3.set(0)
    # 時間
    stime = tkinter.Label(top, text="", font=("Helvetica", 15))
    stime.place(x=110, y=350)
    showtime()
    top.mainloop()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章