sound — Sound effects and music playback

The sound module contains functions for playing simple sound effects on iOS.

Functions

sound.play_effect(name[, volume, pitch])

Play the sound effect with the given name. You can access a list of built-in sound effect names using the asset picker ([+] button in the toolbar), but name can also be a file path if you want to use your own sound effects.

Playback is asynchronous, i.e. the function returns before the sound has finished playing. The return value is an opaque identifier that can be used with the stop_effect() function. The volume and pitch arguments are optional, by default, the effect is played with the global volume, as set by set_volume().

sound.stop_effect(effect_id)

Stops playback of a sound effect. The effect_id is the return value of play_effect().

sound.set_volume(vol)

Sets the default volume for all sound effects (between 0.0 and 1.0, the default is 0.5).

sound.set_honors_silent_switch(flag)

Determines whether the silent (mute) switch is honored when playing sounds (True by default).

Player Class

class sound.Player(file_path)

The Player class provides an easy-to-use interface for playing audio files from disk. Using this class is recommended for music and other audio that doesn’t require very low latency. For sound effects in games, play_effect() is more suitable.

Player Methods

Player.play()

Start playing audio.

Player.stop()

Stop playing audio and reset the playback position.

Player.pause()

Stop playing audio, but keep the current playback position.

Player Attributes

Player.duration

The duration of the loaded audio track in seconds (read-only).

Player.current_time

The current playback position in seconds.

Player.duration

The duration of the audio track (read-only).

Player.number_of_loops

The number of times the audio track should be repeated. Set to -1 to repeat forever.

MIDIPlayer Class

class sound.MIDIPlayer(file_path[, sound_bank_path])

A MIDIPlayer provides simple playback functions for MIDI (.mid) files, using either the built-in “Merlin Silver” sound bank or one that you provide. If you provide a custom sound bank, it must be in sf2 format.

MIDIPlayer Methods

MIDIPlayer.play()

Start playback.

MIDIPlayer.stop()

Stop playback.

MIDIPlayer Attributes

MIDIPlayer.current_time

The current playback position.

MIDIPlayer.duration

The duration of the loaded MIDI file.

MIDIPlayer.rate

The playback rate (1.0 for normal speed).