带有HTML5音频后备的Web音频库。
waud是一个简单而强大的Web音频库,可让您超越HTML5的<audio>标签,并可以轻松地利用Web Audio API。它抽象了Web音频API,使其在多个平台和浏览器中保持一致且可靠。它还返回到不支持Web音频API的非现代浏览器上的HTML5音频。
特征
- base64包
- 音频精灵
- iOS音频解锁1
- 自动静音2
- 简单的API
- 零依赖性
1在初次触摸时自动在iOS设备上自动解锁音频。
2当窗口不在焦点(切换选项卡,最小化窗口等)时,自动静音音频。
安装
可通过NPM,CDN和Haxelib(用于HAXE用户)获得。
- NPM:https://www.npmjs.com/package/waud.js
- CDN:https://cdnjs.com/libraries/waud.js
- haxelib:http://lib.haxe.org/p/waud
API文档
例子
例子:
var snd = new waud Sound ( \"assets/loop.mp3\" , { autoplay : false , loop : true , volume : 0.5 , onload : playBgSound } ) ;
BASE64数据URI
waud支持base64在所有浏览器中解码,包括IE 9,我建议在音频精灵上使用它。
使用waud Base64生成Base64编码的JSON文件。
npm install -g waud base64
waudBase64Pack(\”assets/sounds.json\”, _onLoad);
function _onLoad(snds) {
snds.get(\”assets/beep.mp3\”).play();
}\”>
var base64pack = new waud Base64Pack ( \"assets/sounds.json\" , _onLoad ) ; function _onLoad ( snds ) { snds . get ( \"assets/beep.mp3\" ) . play ( ) ; }
waud还支持将数据uri字符串传递给waud Sound 。
//Note that the data URI used below is a sample string and not a valid sound
var base64Snd = new waud Sound ( \"data:audio/mpeg;base64,//uQxAAAAAAAAAAAAASW5mbwAAAA8AAABEAABwpgADBwsLDxISF\" ) ;
音频精灵
使用waud Sprite生成音频精灵。
npm install -g waud sprite
waudSound(\”assets/sprite.json\”);
audSprite.play(\”glass\”);\”>
var audSprite = new waud Sound ( \"assets/sprite.json\" ) ; audSprite . play ( \"glass\" ) ;
实时音频流
waud广播
waud支持实时音频流,但要确保仅通过HTML5音频播放实时流,因此请确保禁用Web音频。
var snd = new waudSound ( \"http://ice-sov.m*usicra*d*io.com/SmoothUKMP3\" , { autoplay : true , webaudio : false } ) ;
浏览器支持
对所有主要浏览器进行了测试。
| 最新✓ | 最新✓ | 最新✓ | 最新✓ | 9-11✓ | 最新✓ |
浏览器和设备测试提供了:
问题
找到了错误?请创建一个新问题。
演示
- JavaScript
- 哈克斯
- 基础64
用法
waud. Make sure to call this before loading sounds.
waud .init();
// To automatically unlock audio on iOS devices by playing a blank sound.
// The parameter is a callback function that can be used to start playing sounds like background music.
waud .enableTouchUnlock(touchUnlock);
// Use if you want to mute audio when the window is not in focus like switching tabs, minimising window,
// etc in desktop and pressing home button, getting a call, etc on devices.
waud .autoMute();
// Load and play looping background sound with autoPlay and loop set to true.
// Note that this will not play automatically on iOS devices without touching the screen.
var bgSnd = new waud Sound(\”loop.mp3\”, {
\”autoplay\”: true,
\”loop\”: true
});
// Touch unlock callback for iOS devices to start playing bgSnd if it\’s not already playing
function touchUnlock() {
if (!bgSnd.isPlaying()) bgSnd.play();
}\”>
// Initialize waud . Make sure to call this before loading sounds. waud . init ( ) ; // To automatically unlock audio on iOS devices by playing a blank sound. // The parameter is a callback function that can be used to start playing sounds like background music. waud . enableTouchUnlock ( touchUnlock ) ; // Use if you want to mute audio when the window is not in focus like switching tabs, minimising window, // etc in desktop and pressing home button, getting a call, etc on devices. waud . autoMute ( ) ; // Load and play looping background sound with autoPlay and loop set to true. // Note that this will not play automatically on iOS devices without touching the screen. var bgSnd = new waud Sound ( \"loop.mp3\" , { \"autoplay\" : true , \"loop\" : true } ) ; // Touch unlock callback for iOS devices to start playing bgSnd if it\'s not already playing function touchUnlock ( ) { if ( ! bgSnd . isPlaying ( ) ) bgSnd . play ( ) ; }
样本率
- 建议对所有音频文件使用相同的采样率。播放不同的样本率文件可能会在某些设备上引起问题。
- 默认情况下, waud使用44100个样本率。如果您的音频文件具有不同的示例率,则使用
waud .preferredSampleRate进行指定。
许可信息
此内容是根据MIT许可发布的。
行为准则
行为准则改编自贡献者盟约,版本1.4
