Sound Support¶
These classes allow you to play sounds in your game. A sound is a WAV file that can be
played on command via the method play
. While some platforms may support MP3s, we
can only guarantee that WAVs work on all platforms. In order for Kivy to find a WAV
file, you should put it in the Sounds directory. Sounds in that folder can be
referenced directly by name.
When a sound is played, it cannot be played again until it finishes, or is stopped.
This means that if you want multiple, simultaneous sound effects from the same WAV
file you will need to create multiple Sound
objects.
Class Sound¶
This is the primary class. It represents a sounds that can be played.
Constructor¶
- class game2d.Sound(source)¶
A class representing a sound object that can be played.
A sound is a WAV file that can be played on command via the method
play()
. While some platforms may support MP3s, we can only guarantee that WAVs work on all platforms. In order for Kivy to find a WAV or MP3 file, you should put it in the Sounds directory. Sounds in that folder can be referenced directly by name.When a sound is played, it cannot be played again until it finishes, or is stopped. This means that if you want multiple, simultaneous sound effects from the same WAV file.you will need to create multiple Sound objects.
Attributes¶
- Sound.volume¶
The current sound volume.
1 means full volume, 0 means mute. The default value is 1.
Invariant: Must float in the range 0..1.
- Sound.source¶
The source file for this sound.
Immutable: This value cannot be changed after the sound is loaded.
Invariant: Must be a nonempty string.
Methods¶
- Sound.play(loop=False)¶
Plays this sound.
The sound will play until completion, or interrupted by the user.
- Parameters
loop (
bool
) – Whether or not to loop the sound
Class SoundLibrary¶
This class implements to the dictionary interface to make it easier to load sounds and manage them. To load a sound, simply assign it to the library object, as follows:
soundlib['soundname'] = 'soundfile.wav'
The sound library will load the sound and map it to 'soundname'
as the key.
To play the sound, we access it as follows:
soundlib['soundname'].play()