Quick answer: Pygame end-of-music events require mixer.music.set_endevent(SONG_END) and active polling via pygame.event.get(). The event also never fires when play(loops=-1) is used, because the music never ends. Use get_busy() or finite loops if you need to detect completion.

Here is how to fix Pygame mixer.music end events that never deliver. You expect a callback when the song finishes so you can queue the next track, but the loop simply ends with silence and your handler never runs. Pygame uses an event-queue model for music end notifications; you have to set the event id and consume it from the queue.

The Symptom

You set up music playback, expect to know when the track ends, and nothing fires. The track plays out, the mixer goes silent, and your code keeps polling without ever advancing to the next song.

What Causes This

set_endevent never called. Without it, pygame does not post any event when music ends. Music quietly stops and the queue stays empty.

Event queue not polled. Even if set_endevent is configured, the event sits in the queue until pygame.event.get() or pygame.event.poll() consumes it.

Looping infinitely. play(loops=-1) repeats forever — the track never reaches end, so no end event ever posts.

Event id collision. If your code uses USEREVENT directly without an offset, multiple subsystems may post events with the same id and stomp on each other.

The Fix

Step 1: Define a unique end event id.

import pygame

pygame.init()
pygame.mixer.init()

SONG_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(SONG_END)

Use USEREVENT + N where N is unique per subsystem. USEREVENT + 1 for music end, USEREVENT + 2 for a custom timer, and so on.

Step 2: Poll for the event.

pygame.mixer.music.load("assets/song.ogg")
pygame.mixer.music.play()

playlist = ["song1.ogg", "song2.ogg", "song3.ogg"]
idx = 0

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == SONG_END:
            idx = (idx + 1) % len(playlist)
            pygame.mixer.music.load(playlist[idx])
            pygame.mixer.music.play()

Step 3: Use finite loops if you also want event firing.

# loops=-1 -> never ends, no end event
# loops=0  -> play once, end event after
# loops=2  -> play three times, end event after the third
pygame.mixer.music.play(loops=2)

Step 4: Fall back to get_busy polling. If you cannot rely on events (for example, if your code is in a thread without an event loop), poll directly:

if not pygame.mixer.music.get_busy():
    # Music finished, advance playlist
    next_track()

get_busy() returns False when no music is playing, including after the track ends. It works without event-queue polling but does not give you exact timing.

Step 5: Trim trailing silence in audio. If the end event fires later than expected, your file probably has silent tail. Use Audacity or ffmpeg to trim:

ffmpeg -i input.ogg -af silenceremove=stop_periods=-1:stop_duration=0.1:stop_threshold=-50dB output.ogg

Subtleties of get_busy

Calling pygame.mixer.music.pause() makes get_busy() return False even though the track is paused, not finished. Use get_pos() in addition to verify whether the track has actually ended (returns -1 when no track loaded, or 0+ for current ms position).

Streaming formats like OGG can have buffer-related delays at end. The end event posts when the streaming buffer drains, which is typically a few hundred milliseconds after the perceived audio end. For frame-perfect transitions, blend the next track with a short crossfade rather than waiting for end exactly.

“Set the event id, poll the queue, avoid infinite loops if you want to be told when it ends.”

Related Issues

For other Pygame audio issues, see Joystick Not Detected. For draw order issues, see Sprite Group Draw Order.

USEREVENT + 1. set_endevent. Poll the queue. Music plays end-to-end and tells you about it.