pythonpython-vlc

Music Player Python


I m building music player in python. Program working for one file or if i add playlist prepared in advance. I have a question, is there a way to dynamic add file to playlist in vlc lib ? that is add path to file, tick and play this file ? or i have to change lib to 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")


Solution

  • pygame is a very good option for this first before anything do 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()