I am trying to create a MP3-player with Tkinter and PyGame(it's music module). I have written a method to check if the song has finished playing, but the problem is that the programe itself gets cycled in it and I don't know how to implement it differently. Is there any other way to do this?
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
UPDATED
Here is the code from relevant methods:
# 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)