用Python制作属于自己的音乐播放器

2021-08-11 23:03:49

想不想手动打造一款专属于你的播放器,同时练练Python编程?

如果想,那就立即行动吧!

所需库

pygame

tkinter

mutagen

至于它们的使用,可以浏览一下文档,如果你想做更功能强大的播放器,那必须好好看看哦!

设计思路

作为版本0,我们并不想做太复杂的项目。只需实现以下几个功能:

  • 将某个目录下的mp3文件名在listbox中显示

  • 显示当前播放的歌曲名

  • 播放上一首歌曲

  • 播放下一首歌曲

  • 停止播放

获取目录下的mp3文件

直接上代码!

def directorychooser():    directory  = tkinter.filedialog.askdirectory()    os.chdir(directory)    for files in os.listdir(directory):        if files.endswith('.mp3'):            realdir = os.path.realpath(files)            audio = ID3(realdir)            realnames.append(audio['TIT2'].text[0])            listofsongs.append(files)            # print(files)    pygame.mixer.init()    pygame.mixer.music.load(listofsongs[0])    pygame.mixer.music.play()


与button有关的函数

def nextsong(event):    global index    if index < len(listofsongs) - 1:        index += 1    else:        index = 0;    pygame.mixer.music.load(listofsongs[index])    pygame.mixer.music.play()    updatelabel()def previoussong(event):    global index    if index > 0 :        index -= 1    else:        index = len(listofsongs) - 1    pygame.mixer.music.load(listofsongs[index])    pygame.mixer.music.play()    updatelabel()def stopsong(event):    pygame.mixer.music.stop()    v.set("")


更新当前播放歌曲名
def updatelabel():
    global index
    v.set(realnames[index])
最后效果

不太好看,没关系,我们可以一步一步让它功能更强大,更靓!

完整代码:

import os

import pygame

import tkinter

import tkinter.filedialog

from mutagen.id3 import ID3


root = tkinter.Tk()

root.minsize(300, 300)


listofsongs = []

realnames = []

v = tkinter.StringVar()

songlabel = tkinter.Label(root, textvariable=v, width=35)

index = 0



def nextsong(event):

    global index

    if index < len(listofsongs) - 1:

        index += 1

    else:

        index = 0;

    pygame.mixer.music.load(listofsongs[index])

    pygame.mixer.music.play()

    updatelabel()


def previoussong(event):

    global index

    if index > 0 :

        index -= 1

    else:

        index = len(listofsongs) - 1

    pygame.mixer.music.load(listofsongs[index])

    pygame.mixer.music.play()

    updatelabel()


def stopsong(event):

    pygame.mixer.music.stop()

    v.set("")



def updatelabel():

    global index

    v.set(realnames[index])


def directorychooser():

    directory  = tkinter.filedialog.askdirectory()

    os.chdir(directory)


    for files in os.listdir(directory):

        if files.endswith('.mp3'):

            realdir = os.path.realpath(files)

            audio = ID3(realdir)

            realnames.append(audio['TIT2'].text[0])

            listofsongs.append(files)

            # print(files)


    pygame.mixer.init()

    pygame.mixer.music.load(listofsongs[0])

    pygame.mixer.music.play()



directorychooser()


label = tkinter.Label(root, text="Music Player")

label.pack()


listbox = tkinter.Listbox(root)

listbox.pack()

# List of songs

realnames.reverse()

for item in realnames:

    listbox.insert(0, item)

realnames.reverse()


nextbutton = tkinter.Button(root, text='Next Song')

nextbutton.pack()


previousbutton = tkinter.Button(root, text="Previous Song")

previousbutton.pack()


stopbutton = tkinter.Button(root, text="Stop Music")

stopbutton.pack()



nextbutton.bind("<Button-1>", nextsong)

previousbutton.bind("<Button-1>", previoussong)

stopbutton.bind('<Button-1>', stopsong)


songlabel.pack()


root.mainloop()


作者:Viljw

源自:http://www.jianshu.com/p/5bd0dd224209

声明:文章著作权归作者所有,如有侵权,请联系小编删除



Copyright © 2023 All Rights Reserved 版权所有 洛阳宣传音乐虚拟社区