audio — Sound effects and music¶
Sound reference¶
-
class
audio.Sound(sound_filepath, volume=1.0)¶ A
Soundobject represents a sound effect. Multiple sound effects can be played simultaneously.sound_filepath argument must point to a sound file in a compatible format. Currently supported formats are:
wav
ogg
volume paramter must be a value between 0 and 1
Instance properties:
-
Sound.volume¶ Gets or sets a default volume of the sound effect.
Instance methods:
-
Sound.play(volume=1.0)¶ Plays the sound effect.
Volume is a value between 0 and 1. The volume is modified by the master sound volume level setting.
Refer to
engine.AudioManagerdocumentation on how to set the master volume for sounds.Multiple sound effects can be played simultaneously, up to a limit set on the AudioManager.mixing_channels property.
The play() method is a simple “fire and forget” mechanism. It does not allow you to stop, pause or resume the sound. If you need more control on how the sound effects playback, use the
SoundPlaybackwrapper.
SoundPlayback reference¶
-
class
audio.SoundPlayback(sound, volume=1.0)¶ A wrapper class for
Soundobjects, offering more control over sound effects playback.The
soundparameter must be aSoundinstance.Volume must be a value between 0 and 1.
Instance properties:
-
SoundPlayback.status¶ Read only. Returns the sound status, as
AudioStatusenum value.
-
SoundPlayback.is_playing¶ Read only. Returns
Trueif the sound is playing.
-
SoundPlayback.is_paused¶ Read only. Returns
Trueif the sound is paused.
-
SoundPlayback.volume¶ Gets or sets the volume. Must be a number between 0 and 1.
Instance methods:
-
SoundPlayback.play(loops=1)¶ Plays the sound effect.
The
loopsparameter is how many times the sound should play. Set to 0 to play the sound in the infinite loop.Multiple sound effects can be played simultaneously, up to a limit set on the AudioManager.mixing_channels property.
Use
stop(),pause()andresume()methods to control the sound playback.
-
SoundPlayback.stop()¶ Stops the sound playback if it’s playing or paused.
-
SoundPlayback.pause()¶ Pauses the sound playback if it’s playing.
-
SoundPlayback.resume()¶ Resumes the sound playback if it’s paused.
Music reference¶
-
class
audio.Music(music_filepath, volume=1.0)¶ A
Musicobject represents a single music track. There’s more control over playing Music tracks than Sounds as you can pause, resume or stop them on demand. Only one music track can be played at a time.music_filepath argument must point to a soundtrack file in a compatible format. Currently supported formats are:
wav
ogg
Class methods
Instance properties
-
Music.status¶ Read only. Returns the status of the Music track, as
AudioStatusenum value.
-
Music.is_playing¶ Read only. Returns
Trueif the music is playing.
-
Music.is_paused¶ Read only. Returns
Trueif the music is paused.
-
Music.volume¶ Gets or sets a default volume of the music track.
Instance methods
-
Music.play(volume=1.0)¶ Starts playing the music track. If another music track is playing it is automatically stopped.
Volume is a value between 0 and 1. The volume is modified by the master music volume level setting.
Refer to
engine.AudioManagerdocumentation on how to set the master volume for music.
-
Music.pause()¶ Pauses the music track currently being played. Can be resumed with
Music.resume()method
-
Music.resume()¶ Resumes music track paused by
Music.pause(). If the track is not paused, it does nothing.
-
Music.stop()¶ Stops the music track.
AudioStatus reference¶
-
class
audio.AudioStatus¶ Enum type used for referencing sound or music status when working with
Music,SoundandSoundPlaybackobjects. It has the following values:AudioStatus.playingAudioStatus.pausedAudioStatus.stopped