Skip to content

Instantly share code, notes, and snippets.

@huynhbaoan
Created November 14, 2024 06:14
Show Gist options
  • Save huynhbaoan/4cbae8fcc7a82c0dacc896b007b33978 to your computer and use it in GitHub Desktop.
Save huynhbaoan/4cbae8fcc7a82c0dacc896b007b33978 to your computer and use it in GitHub Desktop.
def play_next_track(self):
if not self.playlist:
self.playing = False
return "No tracks available"
try:
self.current_track = self.playlist[0] # Always play the first track in the playlist
self.playlist.append(self.playlist.pop(0)) # Move it to the end of the playlist
pygame.mixer.music.load(self.current_track)
pygame.mixer.music.play()
threading.Thread(target=self._monitor_track, daemon=True).start()
return f"Now playing: {os.path.basename(self.current_track)}"
except Exception as e:
return f"Error playing {os.path.basename(self.current_track)}: {str(e)}"
def _monitor_track(self):
"""Monitor the current track and play the next one when it ends."""
while self.playing: # Check if the player is still in playing mode
if not pygame.mixer.music.get_busy() and not self.paused:
# If music is not playing and not paused, move to the next track
self.play_next_track()
time.sleep(1) # Check every second
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment