pygame.mixer.music
pygame module for controlling streamed audio
pygame.mixer.music.load Load a music file for playback
pygame.mixer.music.unload Unload the currently loaded music to free up resources
pygame.mixer.music.play Start the playback of the music stream
pygame.mixer.music.rewind restart music
pygame.mixer.music.stop stop the music playback
pygame.mixer.music.pause temporarily stop music playback
pygame.mixer.music.unpause resume paused music
pygame.mixer.music.fadeout stop music playback after fading out
pygame.mixer.music.set_volume set the music volume
pygame.mixer.music.get_volume get the music volume
pygame.mixer.music.get_busy check if the music stream is playing
pygame.mixer.music.set_pos set position to play from
pygame.mixer.music.get_pos get the music play time
pygame.mixer.music.queue queue a sound file to follow the current
pygame.mixer.music.set_endevent have the music send an event when playback stops
pygame.mixer.music.get_endevent get the event a channel sends when playback stops

The music module is closely tied to pygame.mixerpygame module for loading and playing sounds. Use the music module to control the playback of music in the sound mixer.

The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.

Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead.

pygame.mixer.music.load()
Load a music file for playback
load(filename) -> None
load(object) -> None

This will load a music filename/file object and prepare it for playback. If a music stream is already playing it will be stopped. This does not start the music playing.

pygame.mixer.music.unload()
Unload the currently loaded music to free up resources
unload() -> None

This closes resources like files for any music that may be loaded.

New in pygame 2.0.0.

pygame.mixer.music.play()
Start the playback of the music stream
play(loops=0, start=0.0, fade_ms = 0) -> None

This will play the loaded music stream. If the music is already playing it will be restarted.

Parameters:
  • loops (int) -- (optional) How many times to repeat the music after it plays once. Setting it to 5 will play the music once, then repeat it 5 more times, for a total of six times. Set to -1 to make the music repeat indefinately.
  • start (float) -- (optional) The position where the music starts playing from. The starting position depends on the format of the music played. MP3 and OGG use the position as time in seconds. For MOD music it is the pattern order number. Passing a start position will raise a NotImplementedError if the start position cannot be set.
  • fade_ms (int) -- (optional) Make the music start playing at 0 volume and fade up to full volume over the given time. The sample may end before the fade-in is complete. Added in pygame 2.0.
pygame.mixer.music.rewind()
restart music
rewind() -> None

Resets playback of the current music to the beginning.

pygame.mixer.music.stop()
stop the music playback
stop() -> None

Stops the music playback if it is currently playing.

pygame.mixer.music.pause()
temporarily stop music playback
pause() -> None

Temporarily stop playback of the music stream. It can be resumed with the pygame.mixer.music.unpause() function.

pygame.mixer.music.unpause()
resume paused music
unpause() -> None

This will resume the playback of a music stream after it has been paused.

pygame.mixer.music.fadeout()
stop music playback after fading out
fadeout(time) -> None

Fade out and stop the currently playing music.

Parameters:time (int) -- Time in milliseconds during which the music volume will fade out before it stops.
Note, that this function blocks until the music has faded out. Calls to fadeout() and
set_volume() will have no effect during this time. If an event was set using set_endevent() it will be called after the music has faded.
pygame.mixer.music.set_volume()
set the music volume
set_volume(volume) -> None

Set the volume of the music playback.

Parameters:volume (float) -- The value argument should be between 0.0 and 1.0. When new music is loaded the volume is reset to full volume.
pygame.mixer.music.get_volume()
get the music volume
get_volume() -> value

Returns the current volume for the mixer. The value will be between 0.0 and 1.0.

pygame.mixer.music.get_busy()
check if the music stream is playing
get_busy() -> bool

Returns True when the music stream is actively playing. When the music is idle this returns False. It returns True even if the music is paused.

pygame.mixer.music.set_pos()
set position to play from
set_pos(pos) -> None

This sets the position in the music file where playback will start. The meaning of "pos", a float (or a number that can be converted to a float), depends on the music format.

For MOD files, pos is the integer pattern number in the module. For OGG it is the absolute position, in seconds, from the beginning of the sound. For MP3 files, it is the relative position, in seconds, from the current position. For absolute positioning in an MP3 file, first call rewind().

Other file formats are unsupported. Newer versions of SDL_mixer have better positioning support than earlier ones. An SDLError is raised if a particular format does not support positioning.

Function set_pos() calls underlining SDL_mixer function Mix_SetMusicPosition.

New in pygame 1.9.2.

pygame.mixer.music.get_pos()
get the music play time
get_pos() -> time

This gets the number of milliseconds that the music has been playing for. The returned time only represents how long the music has been playing; it does not take into account any starting position offsets.

pygame.mixer.music.queue()
queue a sound file to follow the current
queue(filename) -> None

This will load a sound file and queue it. A queued sound file will begin as soon as the current sound naturally ends. Only one sound can be queued at a time. Queuing a new sound while another sound is queued will result in the new sound becoming the queued sound. Also, if the current sound is ever stopped or changed, the queued sound will be lost.

The following example will play music by Bach six times, then play music by Mozart once:

pygame.mixer.music.load('bach.ogg')
pygame.mixer.music.play(5)        # Plays six times, not five!
pygame.mixer.music.queue('mozart.ogg')
pygame.mixer.music.set_endevent()
have the music send an event when playback stops
set_endevent() -> None
set_endevent(type) -> None

This causes pygame to signal (by means of the event queue) when the music is done playing. The argument determines the type of event that will be queued.

The event will be queued every time the music finishes, not just the first time. To stop the event from being queued, call this method with no argument.

pygame.mixer.music.get_endevent()
get the event a channel sends when playback stops
get_endevent() -> type

Returns the event type to be sent every time the music finishes playback. If there is no endevent the function returns pygame.NOEVENT.




Edit on GitHub