Mp3 播放器 - 每次播放文件时都会出现“找不到文件"消息框.showerror [英] Mp3 player - the “file not found” messagebox.showerror shows up every time I play a file
问题描述
下面的代码是一个媒体播放器 (mp3).它运行良好,直到我添加了 show_details() 函数,该函数应该显示歌曲的长度和时间,播放文件时出现错误消息找不到文件"(文件确实播放!但错误消息每次都会弹出).一旦我去掉由 'a'、'total_length' 和 'print' 语句组成的 3 行,错误就会停止出现,歌曲当然可以正常播放.我的代码有什么问题?
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')
推荐答案
我看到您同时创建了一个音乐和声音对象.这可能是您答案的线索:如何在在 pygame 中的同一时间?
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)
这篇关于Mp3 播放器 - 每次播放文件时都会出现“找不到文件"消息框.showerror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!