webaudio — Play audio
Load and play audio
Functions
webaudio.loadAudio(sources[, autoplay=False])
: Load an audio, this will return an Audio object (see Audio class below). Parameter sources could be a string (either an url or builtin-audio's name), or a list, which contains a list of url, you may want to use list when you have different format audio, like .mp3, .ogg, .wav. If you pass url as parameter, it must end with .mp3 or .ogg or .wav. The second parameter autoplay, if you set it to True, the audio will auto be played after loaded.webaudio.unloadAudio(audioObject)
: Unload an audio object. An audio object can be generated by using the Audio class (see below).webaudio.unloadAllAudio()
: Unload all loaded audios.webaudio.builtinAudios()
: Print a list of builtin-audio's name.
Class
webaudio.Audio(sources[, autoplay=False])
: Parameter sources and autoplay are the same as loadAudio function (see above). To get an Audio object, you can call it likemyAudio = Audio('http://example.com/sound.mp3', True)
And an Audio object has following method:
myAudio.play([loop=False])
: Play the audio. If pass True as parameter, it will loop playing.myAudio.pause()
: Pause the audio.myAudio.volume()
: Get the current volume number.myAudio.setVolume(volume)
: Set the volume number (from 0 to 1).myAudio.unload()
: Unload audio.
Example
import webaudio
import time
print "Builtin Audios:", webaudio.builtinAudios()
spring_weather = "http://www.soundjay.com/ambient/spring-weather-1.mp3"
# load spring_weather, autoplay it
bgsound = webaudio.loadAudio(spring_weather, True)
# load coin drop use Audio class, this is a builtin-audio
coinsound = webaudio.Audio("soundeffect-coin-drop1")
coinsound.play()
# set background sound volume
bgsound.setVolume(0.5)
# after 5 seconds, pause it
time.sleep(5)
bgsound.pause()
# after 5 seconds, continue playing
time.sleep(5)
bgsound.setVolume(1)
bgsound.play()
# after 5 seconds, unload the bgsound audio
time.sleep(5)
bgsound.unload()
# unload all audio
webaudio.unloadAllAudio()
Royalty-free sound website reference
- soundjay.com - Free Sound Effects.
- www.bfxr.net - Make sound effect for your game.
Credits
Currently the builtinAudios are from soundjay.com