问题描述
我正在尝试使用 Tkinter 和 PyGame(它是音乐模块)创建 MP3 播放器。我写了一个方法来检查歌曲是否播放完毕,但问题是程序本身在其中循环,我不知道如何以不同的方式实现它。有没有其他方法可以做到这一点?
self.soNG_END = pygame.USEREVENT
pygame.mixer.music.set_endevent(self.soNG_END)
def __wait_for_song_to_end(self):
while True:
for event in pygame.event.get():
if event.type == self.soNG_END:
self.__queue_song()
self.__set_new_selection() #works with listBox
break
# Gets called when the 'play' button gets pressed
def play_song(self):
if not pygame.mixer.music.get_busy() and self.isPaused:
pygame.mixer.music.unpause()
self.isPaused = False
self.btn_pause.config(image=self.img_pause)
return
if len(self.playlist.songsFullPath) > 0:
# Getting song's full path
for path in self.playlist.songsFullPath:
if path.find(self.playlist.songsList.get(tk.ACTIVE)) != -1:
self.playlist.currentSongName = path
self.playlist.currentSongIndex = self.playlist.songsList.curselection()[0]
break
# Loading the song into the mixer
pygame.mixer.music.load(self.playlist.currentSongName)
# Playing the song
pygame.mixer.music.play(loops=0)
self.songIsLoaded = True
def __queue_song(self):
if self.isOnRepeat:
pygame.mixer.music.queue(self.playlist.currentSongName)
return
if self.playlist.songsListSize > 1 and self.playlist.currentSongIndex < self.playlist.songsListSize - 1:
queuedSong = self.playlist.songsFullPath[self.playlist.songsList.curselection()[0] + 1]
self.playlist.currentSongIndex += 1
pygame.mixer.music.queue(queuedSong)
解决方法
我推荐阅读Tkinter understanding mainloop。
不要等待歌曲完成,不断检查是否已完成。在主循环中添加一个事件循环并在 SONG_END
事件发生时设置一个状态变量:
song_end = False
在主循环中:
for event in pygame.event.get():
if event.type == self.SONG_END:
song_end = True
if song_end:
# [...]
,
您可以做的是,使用 root.after()
调用该函数,以便不断检查该函数是否已结束歌曲。
import pygame
# Same codes and imports
root = Tk() # Don't forget this
pygame.init() # Use this instead of pygame.mixer.init() as you need event
...
def check_event():
for event in pygame.event.get():
if event.type == MUSIC_END:
play() # In your case,run self.__queue_song() and self.__set_new_selection()
root.after(100,check_event) # Repeat this function every 0.1 second
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
play() # Play the first random song
check_event() # Start checking for the end of song
如果你能提供更相关的代码,那么我可以举一个更好的例子。目前,您应该能够重写它以适应您的代码。