- Notifications
You must be signed in to change notification settings - Fork29
Browser sound library which blended HTMLVideo and HTMLAudio and WebAudio
License
CyberAgent/boombox.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
日本語ドキュメントはこちら
"boombox.js" provides unified audio API for browser, such asHTMLVideo /HTMLAudio /WebAudio.
This library has simple API, likeboombox.
The browser correspondence table (2014-03-18) ishere
Web browseres haveHTMLAudio /WebAudio /HTMLVideo API for playing audio. However these API have diffrent way to use and browser have no compatibility.boombox.js provides unified API and solves diffrence enviroment between web browser.
In addition to this library has function for mobile web browser support, eg: stop to play sound when a web browser is inactive, handle multiple audio source.
WebAudio API has advanced feature like "Mixing sound" butboombox.js doesn't extend these feature. Because the library suppose basic usage.Howeverboombox.js doesn't restrict these features, you can extendboombox.js function.
- Play
- Pause
- Stop
- Replay
- Resume
- PowerON/PowerOFF
- Volume
- LoopReproducing
- Playing Multi Sound
boombox.jssupports this feature as far as possible but depends on web browser support.
- CORS Settings
boombox.jshas several configurations to observeCORS specifications.
- Filterings
boombox.jsdivided out sound souces based on browser detection.
- audiosprite is available and generation command.(boombox-audiosprite)
- HTMLAudio/HTMLVideo/WebAudio support
- File size is small(6kb at gzipped).
| OS/Browser | HTMLAudio or HTMLVideo load event |
|---|---|
| IOS 5: Safari | suspend |
| IOS 6, 7: Safari | suspend |
| Android 2.3: basic | stalled |
| Android 4.0: basic | loadeddata |
| Mac OSX: Chrome | canplay |
====
| OS/Browser | Web Audio | HTML Audio | HTML Video |
|---|---|---|---|
| IOS 5 : Safari/chrome | - | ✔ | ✔ *1 |
| IOS 6/7 : Safari/chrome | ✔ | ✔ | ✔ *1 |
| Android 2.3 : basic | - | ✔ | ✔ |
| Android 4.0 : basic | ✔ *2 | ✔ | ✔ |
| Android 4.0 : chrome | ✔ *2 | ✔ | ✔ |
| OS/Browser | Web Audio | HTML Audio | HTML Video |
|---|---|---|---|
| Windows: IE | - | ✔ | ✔ |
| Windows: Chrome | ✔ | ✔ | ✔ |
| Windows: Firefox | ✔ | ✔ | ✔ |
| Windows: Opera | ✔ | ✔ | ✔ |
| Mac OSX: Safari | ✔ | ✔ | ✔ |
| Mac OSX: Chrome | ✔ | ✔ | ✔ |
| Mac OSX: Firefox | ✔ | ✔ | ✔ |
| Mac OSX: Opera | ✔ | ✔ | ✔ |
*1Can't support for starting another application.
*2Some smart phones are supported.
====
| OS/Browser | 1 sound | 2 sound | multi sound |
|---|---|---|---|
| IOS 5: Safari | ✔ | - | - |
| IOS 6, 7: Safari | ✔ | ✔ | ✔ |
| Android 2.x: basic | ✔ | ✔ | ✔ |
| Android 4.x: basic | ✔ | ✔ *1 | - |
| Android 4.x: chrome | ✔ | ✔ | ✔ |
*1HTMLAudio/HTMLVideo are used in combination.
You can downloadboombox.js orboombox.min.js from following links.
$ npm install boombox.js
$ bower install boombox.js
$ component install CyberAgent/boombox.js
Loadboombox.js usingscript tag after download it.
<scripttype="text/javascript"src="YOUR/PATH/TO/boombox.js"></script><!-- for development --><scripttype="text/javascript"src="YOUR/PATH/TO/boombox.min.js"></script><!-- for product -->
boombox.jssupportsrequire.js
UseGrunt for build.
$ git clone https://github.com/CyberAgent/boombox.js.git$cd boombox$ npm install -g grunt-cli# If you haven't already installed grunt-cli.$ npm install.# If you haven't already installed local npm.$ npm run build
You can testboombox.js usingGrunt &beez-foundation.
$ npm install -g beez-foundation# If you haven't already installed beez-foundation that is web server.$cd boombox$ npm install.# Once at first if you don't install local npm.$ npm run start# Starting local server.
@seebeez
@seebeez-foundation
Access in your favorite browser
If you load
boombox.jsin script tag:http://0.0.0.0:1109/m/boombox/spec/global.htmlIf you load
boombox.jsusing require.js :http://0.0.0.0:1109/m/boombox/spec/requirejs.html
Setup method for usingboombox.js.
boombox.setup();
The option for forced use specify format of the sound sources.
{webaudio:{use:true},htmlaudio:{use:true},htmlvideo:{use:true}}
Load the sound sorce.
varoptions={src:[{media:'audio/mp4',path:'http://0.0.0.0:1109/m/spec/media/sound.m4a'}]};boombox.load('sound',options,function(err,audio){// callback function});
You can set multiple sound sources in
options.src,boombox.jsevaluateoptions.srcfrom the beginningthen load sources if available to use.
boombox.play()// All of loaded sounds.boombox.get('sound').play()// Play specify sound.
Many of mobile web browseres can't play sounds without user operation. (MouseEvents etc.)This restriction depends on web browseres specification, It is better to check the web browseres to be used.
boombox.get.volume(0.5)// All sound . Argument takes between 0 to 1.boombox.get('sound').volume(0.5)// Specified sound. Argument takes between 0 to 1.
Turn off sounds whenever playing sound. (like actualyboombox :p)
boombox.get('sound').power(boombox.POWER_OFF);
boombox.power(boombox.POWER_OFF);
There are two ways for loop playback.
- Native loop (This function uses the API supported in
HTMLAudio/WebAudio/HTMLVideo) - Original loop (This function uses
onEndedevent for continuous playback loop)
boombox.get('sound').setLoop(boombox.LOOP_ORIGINAL);boombox.get('sound').play();// orboombox.get('sound').setLoop(boombox.LOOP_NATIVE);boombox.get('sound').play();
This event is called when a sound have been played to the end.
You should override for using this.
boombox.get('name').onEnded=function(){// callback function}
- You can choose which web broswers and smart phones to play sounds
- You can specify multiple filters, but if any filter is NG, boombox quit evaluation immediately
- The callback which is set in
boombox.load()will be called immediately if filters are NG.
boombox.addFilter('chrome',functionfilter(){if(/Chrome/.test(window.navigator.userAgent)){returnfalse;// [ OK ] Chrome}else{returntrue;// [ NG ] Another browseres}});varoptions={src:[{media:'audio/mp4',path:'http://0.0.0.0:1109/m/spec/media/sound.m4a'}],filter:["chrome"]// Assign the filters you want to use.};boombox.load('sound',options,true,function(err,audio){// load sound resources.});
Youshould control to play sounds individually because web browseres have large diffrence in audiio API support.
You can utilizeHTMLVideo when passtrue toboombox.load()'s '3rd argument.
The case you may use this feature when
HTMLAudiosupport only one note to play.
varoptions={src:[{media:'audio/mp4',path:'http://0.0.0.0:1109/m/spec/media/sound.m4a'}]};// Pass `true` to 3rd argument in `boombox.load()`boombox.load('sound',options,true,function(err,audio){// load sound resources.// You should append DOM elements. In case for no visual element, you put DOM in outside display area.});
boombox.pool cache the sound source when loaded.
Web browseres have diffrent behaviour about cache, so this functioin is very effective especially in SPA.
The priority order for using API by default are following.
WebAudio >HTMLAudio >HTMLVideo
- If web browser has both
WebAudioandHTMLAudiothenWebAudiohas priority. boombox.jsdoesn't useHTMLVideoas long as it doesn't enable in option.
boombox.js ignorescurrentTime when web browser can't set seek setting onHTMLAudio andHTMLVideo.:w
boombox.js can judge state that the browser has become background usingwindow.onpageshow/onpagehide,window.onblur/onfocus andEvent.onVisibilityChange.
It is not obtainable on all browser, so you may avouch in your web browser.
All customizable events are named byonXXXX, so you can override these events.
This event is fired this event by the occurrence ofvisibilityChange event.
This event is fired this event by the occurrence ofwindow.onFocus event.
This event is fired this event by the occurrence ofwindow.onBlur event.
This event is fired this event by the occurrence ofwindow.onpageshow event.
This event is fired this event by the occurrence ofwindow.onpagehide event.
This event is fired when a sound have been played to the end.It will not be fired when the sound stops on the way.
// Simple usage.boombox.onFocus=function(e){logger.trace('onFocus');}// Override function.varfn=boombox.onFocus;boombox.onFocus=function(){console.log("override:",onFocus);fn.apply(boombox,arguments);}
boombox.js now supportsaudiosprite. (HTMLAudio/HTMLVideo/WebAudio)
boombox.js can play with one note per one sound source.
boombox.js creates instances of HTMLAudio/HTMLVideo as same as numberes of sprited sounds, but it refer the HTMLAudioElement/HTMLVideoElement as DOM element of the same.
boombox.get("bgm-c2a")===boombox.get("bgm-c3a")// falseboombox.get("bgm-c2a").$el===boombox.get("bgm-c3a").$el// true
boombox.js can play with multiple notes per one sound source.
You can create audio sprite withboombox-audiosprite, related project ofboombox.js
$ npm install -g boombox-audiosprite$ cd {AUDIO_DIRECTORY}┗ $ tree ..├── c5a.wav├── c6a.wav└── c7a.wav# Please see options page of boombox-audiosprite.$ boombox-audiosprite -e ac3,caf,mp3,m4a ./*.wav┗ $ tree ..├── boombox-sprite.json├── c5a.wav├── c6a.wav├── c7a.wav├── sprite.ac3├── sprite.json├── sprite.m4a└── sprite.mp3# JSON data for boombox.js$ cat boombox-output.json{ "spritemap": { "c5a": { "start": 0, "end": 5.990770975056689 }, "c6a": { "start": 7, "end": 12.990770975056689 }, "c7a": { "start": 14, "end": 19.99077097505669 } }, "src": [ { "media": "audio/ac3", "path": "sprite.ac3" }, { "media": "audio/mpeg", "path": "sprite.mp3" }, { "media": "audio/mp4", "path": "sprite.m4a" } ]}<!DOCTYPE HTML><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no"><scriptsrc="boombox.js"></script><script>varoptions={"spritemap":{"c5a":{"start":0,"end":5.990770975056689},"c6a":{"start":7,"end":12.990770975056689},"c7a":{"start":14,"end":19.99077097505669}},"src":[{"media":"audio/ac3","path":"spec/media/sprite/a/sprite.ac3"},{"media":"audio/mpeg","path":"spec/media/sprite/a/sprite.mp3"},{"media":"audio/mp4","path":"spec/media/sprite/a/sprite.m4a"}]};boombox.setup();boombox.load('bgm',options,function(err,audio){console.log(boombox.pool);// load sound data});</script></head><body><buttononclick="boombox.get('bgm-c7a').play();">bgm-c7a play</button></body></html>
boombox.get('bgm-' + sprite name)method can get the individual sound source in audio sprite.
====
spec/media/sound.m4aspec/media/sound.wavspec/media/sprite/a/c5a.wavspec/media/sprite/a/c6a.wavspec/media/sprite/a/c7a.wavspec/media/sprite/b/c5b.wavspec/media/sprite/b/c6b.wavspec/media/sprite/b/c7b.wavspec/media/sprite/a/sprite.ac3spec/media/sprite/a/sprite.m4aspec/media/sprite/a/sprite.mp3spec/media/sprite/b/sprite.ac3spec/media/sprite/b/sprite.m4aspec/media/sprite/b/sprite.mp3spec/media/sprite/c/sprite.ac3spec/media/sprite/c/sprite.m4aspec/media/sprite/c/sprite.mp3
Sound that was created in AudioSauna
- Kei FUNAGAYAMA -@fkeigithub
- Masaki Sueda - github
- HIRAKI Satoru - github
- Kazuma MISHIMAGI -@maginemugithub
CyberAgent, Inc. All rights reserved.
@see :LICENSE
The MIT License (MIT)Copyright © CyberAgent, Inc. All Rights Reserved.Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.About
Browser sound library which blended HTMLVideo and HTMLAudio and WebAudio
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.



