cocos.audio.pygame.music module

Pygame module for controlling streamed audio

The music module is closely tied to pygame.mixer. 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.

fadeout(time)

Stop music playback after fading out.

This will stop the music playback after it has been faded out over the specified time (measured in milliseconds). Any queued music will be unqueued.

Note, that this function blocks until the music has faded out.

Parameters
timeint

Time to fade out over, in milliseconds.

get_busy()

Check if the music stream is playing.

Returns True when the music stream is actively playing. When the music is idle this returns False.

Return type

bool

get_endevent()

Get the event a channel sends when playback stops.

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

Return type

int

get_pos()

Get the amount of time music has been playing.

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.

Returns -1 if the position is unknown.

Return type

int

get_volume()

Get the music volume.

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

Return type

float

load(filename)

Load a music file for playback.

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

Music can only be loaded from filenames, not python file objects like the other pygame loading functions.

Parameters
filenamestr

Filename of music to load.

pause()

Temporarily stop music playback.

Temporarily stop playback of the music stream. It can be resumed with the unpause function.

play(loops=0, start=0.0)

Start the playback of the music stream.

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

The loops argument controls the number of repeats a music will play. play(5) will cause the music to played once, then repeated five times, for a total of six. If loops is -1 then the music will repeat until stopped.

The start argument controls where in the music the song starts playing. The starting position is dependent on the format of music playing. MP3 and OGG use the position as time (in seconds). MOD music it is the pattern order number. Passing a value to start will raise a NotImplementedError if it cannot set the start position

Parameters
loopsint

Number of times to repeat music after initial play through.

startfloat

Starting time within music track to play from, in seconds.

queue(filename)

Queue a music file to follow the current one.

This will load a music file and queue it. A queued music file will begin as soon as the current music naturally ends. If the current music is ever stopped or changed, the queued song 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')
Parameters
filenamestr

Filename of music file to queue.

rewind()

Restart music.

Resets playback of the current music to the beginning.

set_endevent(eventtype=None)

Have the music send an event when playback stops.

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.

Parameters
eventtypeint

Type of event to post. For example, SDL_USEREVENT + n

set_volume(volume)

Set the music volume.

Set the volume of the music playback. The value argument is between 0.0 and 1.0. When new music is loaded the volume is reset.

Parameters
volumefloat

Volume of music playback, in range [0.0, 1.0].

stop()

Stop the music playback.

Stops the current music if it is playing. Any queued music will be unqueued.

unpause()

Resume paused music.

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