Audia reimplements and extends the HTML5 Audio object using the Web Audio API.
-
Future-proof
-
Fails gracefully
-
Consistent API with HTML5 Audio which you probably already know
-
Fixes bugs in some Audio implementations
-
Implementation of
Audiois weak across the board. Even in the best browers -
this is future-proof. you get Audio which is the best you can get for now, then later down the road you get web audio api
-
and seriously, you'll need a wrapper for WAI anyway
A complete write-up on this project can be found on the Lost Decade Games blog.
Everything is identical to the HTML5 Audio spec. Anything not working as it should? File an issue! :)
In fact in many cases Audia is BETTER than the browser's native Audio implementation, even if it doesn't also support Web Audio API.
TODO: Provide thin wrappers around the base code.
-
Audia (global object)
-
version:
StringThe version of Audia being run. (Example:"0.1.0") -
canPlayType: you can pass in mp3, ogg (helpers since normally it wants audio/ogg audio/mp3)
Audia also has the below API. If any of the below functionality is not supported by the client, Audia will fail silently.
Example: var sound = new Audia();
Each Audia instance has the following properties:
- currentTime:
NumberThe playback point of the sound (in seconds). - duration:
NumberThe length of the current sound buffer in seconds. (Read-only) - loop:
BooleanIf set to true, the audio will play again when it reaches the end of playback. (default:false) - muted:
BooleanTrue if the sound has been muted, otherwise false. - paused:
BooleanTrue if the sound is paused, false if it's playing. (Read-only) - src:
StringThe URL of a sound file to load. - volume:
NumberThe volume of the playback where0is muted and1is normal volume. (arbitrary maximum =10), (default:1) - onended:
FunctionGets called when playback reaches the end of the buffer. - onload:
FunctionGets called when a sound file (requested by settingsrc) is done loading.
* The italicized properties are only available if the client supports Web Audio API (otherwise they fail silently).
- play: Begins playback of the sound buffer. Arguments:
currentTime(optional) Sets thecurrentTimeproperty before playing. - pause: Pauses sound playback (retaining
currentTime). - stop: Stops sound playback (resetting
currentTimeto0). // TODO: it's actually .muted (Boolean) - mute: Silences playback of the sound buffer.
- unmute: Restores audible playback of the sound buffer.
var sound = new Audia();
sound.src = "onslaught.mp3";
sound.play();var backgroundMusic = new Audia("joshua_morse.mp3");
var battleMusic = new Audia({
src: "a_recurring_conflict.mp3",
loop: true
});sound.currentTime = 30;var percentage = (sound.currentTime / sound.duration) * 100;if (sound.playing) {
sound.stop();
}sound.onload = function () {
doSomething();
};
sound.src = "new_song.mp3";