buzz
Version:
Buzz, a Javascript HTML5 Audio library
157 lines (117 loc) • 3.71 kB
Markdown
{:.intro} [Buzz](http://buzz.jaysalvat.com) is a small Javascript library that
helps you to easily include and manage sounds in your websites using the
[](https://developer.mozilla.org/en/docs/Web/HTML/Element/audio).
It degrades silently on non-modern browsers.
Create a new sound instance.
```
var mySound = new buzz.sound("/sounds/mysound.ogg");
```
Create a group of sound instances. See [Group section](/documentation/group) for more details.
```
var myGroup = new buzz.group([
new buzz.sound("/sounds/mysound1.ogg"),
new buzz.sound("/sounds/mysound2.ogg"),
new buzz.sound("/sounds/mysound3.ogg")
]);
```
Allow to act on all sound instances. Take a look to the Group section for more details.
```
var mySound1 = new buzz.sound("/sounds/mysound1.ogg"),
mySound2 = new buzz.sound("/sounds/mysound2.ogg"),
mySound3 = new buzz.sound("/sounds/mysound3.ogg");
buzz.all().play();
```
Check if the HTML5 audio tag is supported by the browser.
```
if (!buzz.isSupported()) {
alert("Your browser is too old, time to update!");
}
```
Check if the OGG audio format is supported by the browser.
```
if (!buzz.isOGGSupported()) {
alert("Your browser doesn't support OGG Format.");
}
```
Check if the WAV audio format is supported by the browser.
```
if (!buzz.isWAVSupported()) {
alert("Your browser doesn't support WAV Format.");
}
```
Check if the MP3 audio format is supported by the browser.
```
if (!buzz.isMP3Supported()) {
alert("Your browser doesn't support MP3 Format.");
}
```
Check if the AAC audio format is supported by the browser.
```
if (!buzz.isAACSupported()) {
alert("Your browser doesn't support AAC Format.");
}
```
Format seconds in an easy to read timer like 00:00 or 00:00:00 if long is set to true.
```
var mySound = new buzz.sound("/sounds/mysound.ogg"),
timer = buzz.toTimer(mySound.getDuration());
document.getElemetById("duration").innerHTML = timer;
```
Convert a timer as 00:00 or 00:00:00 in seconds.
```
var mySound = new buzz.sound("/sounds/mysound.ogg");
mySound.setTime(buzz.fromTimer("00:30"));
```
Calculate percentage from values.
```
var mySound = new buzz.sound("/sounds/mysound.ogg"),
time = mySound.getTime(),
duration = mySound.getDuration(),
percent = buzz.toPercent(time, duration, 2);
document.getElementById("percent").innerHTML = percent;
```
Calculate value from a percentage.
```
var mySound = new buzz.sound("/sounds/mysound.ogg");
mySound.setTime(buzz.fromPercent("20", mySound.getDuration());
```
Object of properties. All the default settings can be set globaly.
```
// Preload the sound — auto, metadata, none
buzz.defaults.preload = 'auto';
// Play the sound when ready — bool
buzz.defaults.autoplay = false;
// Loop the sound — bool
buzz.defaults.loop = false;
// value to display when a time convertion is impossible
buzz.defaults.placeholder = '--';
// Duration of a fading effect — milliseconds
buzz.defaults.duration = 5000;
// Audio formats of your files
buzz.defaults.formats = [ 'ogg', 'mp3', 'aac', 'wav' ];
```
Array of all the sound instances created.
```
for (var i in buzz.sounds) {
buzz.sounds[i].mute();
}
```