问题
The code below is a media player (mp3). It worked perfectly until I added the show_details() function which is supposed to show the length and timing of the song, the error message "file not found" appears when playing a file (The file does play! but the error message pops everytime). Once I take off the 3 lines consisting of 'a', 'total_length' and 'print' statements, the error stops to show up and the song of course plays normally. What is the problem with my code?
filelabel = Label(root, text="Let's make some noise!")
filelabel.pack(pady=10)
lengthlabel = Label(root, text="Total Length - 00:00")
lengthlabel.pack(pady=10)
def show_details():
filelabel['text'] = 'Playing - ' + os.path.basename(filename)
a = mixer.Sound(filename)
total_length = a.get_length()
print (total_length)
def play_music():
global paused
if paused:
mixer.music.unpause()
statusbar['text'] = 'Music Resumed: Playing ' + os.path.basename(filename)
paused = False
else:
try:
mixer.music.load(filename)
mixer.music.play()
statusbar['text'] = 'Playing - ' + os.path.basename(filename)
show_details()
except:
tkinter.messagebox.showerror('FIle not found', 'Melody could not find the song, please check again')
回答1:
I see you create a music and sound object at the same time. This might be a clue to your answer: How can I play multiple sounds at the same time in pygame?
It seems that adding channels might fix your problem. But take out these three lines first to see if they are the cause:
a = mixer.Sound(filename)
total_length = a.get_length()
print (total_length)
来源:https://stackoverflow.com/questions/61780439/mp3-player-the-file-not-found-messagebox-showerror-shows-up-every-time-i-pla