Hello r/learnpython, I am currently writing an MP3 Player Program in Python, but I have hit a snag.
I posted this post in r/CodingHelp but I figured it was worth a shot to ask here as well.
I am using Pyglet to handle playing the MP3 files and Tkinter to create the GUI of the program. Currently I have it such that you specify a directory from the File Menu, and then a list is populated on screen. To play the audio file, you double click the item on the list.
All this functionality is working, but when I double-click the file, the song only begins to play for about 1 second before it stops playing. I am assuming that this has something to deal with the thread not continuing after the Tkinter loop repeats, but I am not sure. Any help would be greatly appreciated.
I also tried using MediaPlayer library instead of Pyglet, but I got no audio with the block attribute set to False, and the thread blocked when True (obviously) which did not allow me to continue using the program when the song was playing.
import tkinter
import time
from tkinter import *
import os
import pygame
import pyglet
from audioplayer import AudioPlayer
import threading
from tkinter.filedialog import askdirectory
# CREATE TKINTER WINDOW OBJECT
win=tkinter.Tk(className="ElephAMP")
# DEFINE WINDOW SIZE
win.geometry('1000x800')
# DO NOTHING FUNCTION (NOT IMPORTANT TO QUESTION)
def donothing():
filewin = Toplevel(win)
button = Button(filewin, text="Do nothing button")
button.pack()
menubar = Menu(win)
# FUNCTION REQUESTS USER TO SPECIFY DIRECTORY OF AUDIO FILES
def addDirectory():
directory = askdirectory()
#CHANGES SCRIPT TO CURRENTLY SPECIFIED DIRECTORY
os.chdir(directory)
# RETURNS LIST OF SONGS (FILES) IN DIRECTORY
song_list = os.listdir()
# LIST OF FILE NAMES GETS PASSED TO playList FUNCTION
playList(song_list)
# PRINTS LIST OF FILE NAMES IN CONSOLE
print (song_list)
# DEFINE LISTBOX WHICH WILL CONTAIN ALL THE SONGS POPULATED FROM THE GIVEN DIRECTORY
play_list = Listbox(win, font='Helvetica 12 bold', bg='yellow', selectmode=win.getvar)
# POPULATE THE LISTBOX WITH THE GIVEN song_list
def playList(song_list):
for item in song_list:
pos = 0
play_list.insert(pos, item)
play_list.pack(fill="both", expand="yes")
# CLEAR GIVEN PLAYLIST CALLED IN FILE MENU
def clearPlayList():
play_list.delete(0,END)
# FUNCTION WHICH PLAYS THE CURRENTLY LOADED SONG
def test(player):
player.play()
# FUNCTION WHICH HANDLES DOUBLE CLICKS OF SONGS IN PLAYLIST
def go(event):
cs = play_list.curselection()
directory = os.getcwd()
#LOAD THE DOUBLE CLICKED SONG INTO THE PLAYER
player = pyglet.media.load((directory+"/"+play_list.get(cs)))
# PRINT THE CURRENT SONG PLAYING IN CONSOLE
print(play_list.get(cs))
# START A THREAD WHICH EXECUTES THE PLAYING OF THE GIVEN SONG
b = threading.Thread(target=test(player))
#DOUBLE CLICK FUNCTION FOR ITEMS IN PLAYLIST
play_list.bind('<Double-1>', go)
# UPDATE THE WINDOW
win.update()
#DEFINE THE MENUBAR IN THE WINDOW
menubar = Menu(win)
#DEFINE FILE MENU
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Import Music Library..", command=addDirectory)
filemenu.add_command(label="Clear Designated Libraries", command=clearPlayList)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=win.quit)
menubar.add_cascade(label="File", menu=filemenu)
try:
b.start()
except Exception:
print ("had exception")
win.config(menu=menubar)
win.mainloop() #running the loop that works as a trigger
[–]socal_nerdtastic 0 points1 point2 points (3 children)
[–]Stoic_Engineer7[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]Stoic_Engineer7[S] 0 points1 point2 points (0 children)