音乐播放器 Python

Music Player Python

我正在 python 中构建音乐播放器。为一个文件工作的程序,或者如果我添加预先准备好的播放列表。我有一个问题,有没有办法将文件动态添加到 vlc lib 中的播放列表?那就是添加文件路径,勾选并播放这个文件?或者我必须将 lib 更改为 pygame ?

root = tk.Tk()
root.title("Python Simple Player")
root.geometry("600x500")

#List of audio files
fileListFrame = tk.Listbox(root, bg="aqua", fg="green", width=60)
fileListFrame.pack(pady=20)

#box to print audioFile tag's
audioTagBox = tk.Listbox(root, bg="white", fg="green", width=60)
audioTagBox.pack(pady=20)

file='Alex2.mp3'

class music_Player():
    def __init__(self, audio_file):
    # creating a vlc instance
        vlc_instance = vlc.Instance()
    # creating a media player
        self.player = vlc_instance.media_player_new()
    # creating a media
        media = vlc_instance.media_new(audio_file)
    # setting media to the player
        self.player.set_media(media)
    
    def btn_play(self): #def play
        song = fileListFrame.get(ACTIVE)
        audio =music_Player(song)
        self.player.play()
    
    def btn_pause(self): #def pauzy
        self.player.pause()
    
    def btn_stop(self): #def stopu
        self.player.stop()

    def btn_next(self):
        pass

    def btn_prev(self):
        pass

audio =music_Player(file)
def add_song():
    song = filedialog.askopenfilename(initialdir='audio/', title="Choose song", filetypes=(("mp3 Files", "*.mp3"), ))
    fileListFrame.insert(END, song)
my_menu = tk.Menu(root)
root.config(menu=my_menu)

file_menu = tk.Menu(my_menu)
my_menu.add_cascade(label = "File", menu = file_menu)
file_menu.add_command(label = "Exit")

pygame 是一个很好的选择 首先在做任何事情之前 pip install pygame

import pygame
class your_app_name(self, tk.Tk):
   def __init__(*args **kwargs):
       super().__init__(*args, **kwargs)
       pygame.init()
       #GUI code instead of root, though you can use self and you don't need to create it either and for using any of these function do self.func_name
   def play(self):
       pygame.mixer.music.load(your_file_name)
       pygame.mixer.music.play()
   def stop(self):
       pygame.mixer.music.stop()
app = your_app_name()#inside of brackets you can pass arguments which you would pass to tk.Tk()
#for doing things like root.geometry() do app.geometry()
app.mainloop()

你知道文件夹路径了吧?那么你可以简单地这样做:

import os
allmp3files=[x for x in os.listdir("folder/path") if x.endswith(".mp3")]

现在您获得了该目录中所有 mp3 文件的列表。现在您可以像往常一样使用 allmp3files[0/1/2/3....] 了。或者有你的 fileListFrame(Menu) 你可以简单地在那个菜单中添加所有这些文件,然后......