howler.js音频控制js库

下载地址:https://github.com/goldfire/howler.js

Features 

  • Defaults to Web Audio API
  • Falls back to HTML5 Audio
  • Supports multiple file formats to support all browsers
  • Automatic caching for Web Audio API
  • Implements cache pool for HTML5 Audio
  • Per-sound and global mute/unmute and volume control
  • Playback of multiple sounds at the same time
  • Easy sound sprite definition and playback
  • Fade in/out sounds
  • Methods can be chained
  • Uses no outside libraries, just pure Javascript
  • Lightweight, 9kb filesize (3kb gzipped)

Documentation

Examples

Most basic, play an MP3/OGG:  
       
var sound = new Howl({
  urls: ['sound.mp3', 'sound.ogg']
}).play();

More playback options:  
       
var sound = new Howl({
  urls: ['sound.mp3', 'sound.ogg', 'sound.wav'],
  autoplay: true,
  loop: true,
  volume: 0.5,
  onend: function() {
    alert('Finished!');
  }
});

Define and play a sound sprite:  
     
var sound = new Howl({
  urls: ['sounds.mp3', 'sounds.ogg'],
  sprite: {
    blast: [0, 2000],
    laser: [3000, 700],
    winner: [5000, 9000]
  }
});

// shoot the laser!
sound.play('laser');

Properties  
  • autoplayBoolean (true by default) Set to true to automatically start playback when sound is loaded.
  • bufferBoolean (false by default) Set to true to force HTML5 Audio. This should be used for large audio files so that you don't have to wait for the full file to be downloaded and decoded before playing.
  • formatString (null by default) howler.js automatically detects your file format from the URL, but you may also specify a format in situations where URL extraction won't work.
  • loopBoolean (false by default) Set to true to automatically loop the sound forever.
  • spriteObject ({} by default) Define a sound sprite for the sound. The offset and duration are defined in milliseconds.
// Sound sprite definition format
{
  key: [offset, duration]
}
  • volumeNumber (1.0 by default) The volume of the specific track, from 0.0 to 1.0.
  • urlsArray ([] by default) The source URLs to the track(s) to be loaded for the sound. These should be in order of preference, howler.js will automatically load the first one that is compatible with the current browser.
  • onendFunction (function(){} by default) Fire when the sound finishes playing (if it is looping, it'll fire at the end of each loop).
  • onloadFunction (function(){} by default) Fires when the sound is loaded.
  • onloaderrorFunction (function(){} by default) Fires when the sound fails to load.
  • onpauseFunction (function(){} by default) Fires when the sound has been paused.
  • onplayFunction (function(){} by default) Fires when the sound begins playing.

Methods  
  • play: Begins playback of sound. Will continue from previous point if sound has been previously paused.
    • spriteString (optional) Plays from the defined sprite key.
  • pause: Pauses playback of sound, saving the pos of playback.
    • idNumber (optional) The play instance ID.
  • stop: Stops playback of sound, resetting pos to 0.
    • idNumber (optional) The play instance ID.
  • mute: Mutes the sound, but doesn't pause the playback.
    • idNumber (optional) The play instance ID.
  • unmute: Unmutes the sound.
    • idNumber (optional) The play instance ID.
  • fadeIn: Fade in the current sound.
    • toNumber Volume to fade to (0.0 to 1.0).
    • durationNumber Time in milliseconds to fade.
    • callbackFunction (optional) Fires when fade is complete.
  • fadeOut: Fade out the current sound and pause when finished.
    • toNumber Volume to fade to (0.0 to 1.0).
    • durationNumber Time in milliseconds to fade.
    • callbackFunction (optional) Fires when fade is complete.
    • idNumber (optional) The play instance ID.
  • loop: Get/set whether to loop the sound.
    • loopBoolean (optional) To loop or not to loop, that is the question.
  • pos: Get/set the position of playback.
    • positionNumber (optional) The position to move current playback to.
    • idNumber (optional) The play instance ID.
  • sprite: Get/set sound sprite definition.
    • spriteObject (optional) See above for sound sprite definition.
  • pos3d: Get/set the 3D position of the audio source. The most common usage is to set the x position to affect the left/right ear panning. Setting the value higher than 1.0 will begin to decrease the volume of the sound as it moves further away. This only works with Web Audio API.
    • xNumber The x-position of the sound.
    • yNumber The y-position of the sound.
    • zNumber The z-position of the sound.
    • idNumber (optional) The play instance ID.
  • volume: Get/set volume of this sound.
    • volumeNumber (optional) Volume from 0.0 to 1.0.
    • idNumber (optional) The play instance ID.
  • urls: Get/set the URLs to be pulled from to play in this source.
    • urlsArray (optional) Changes the source files for this Howl object.
  • on: Call/set custom events. Multiple events can be added by calling this multiple times.
    • eventString Name of event to fire/set.
    • functionFunction (optional) Define function to fire on event.
  • off: Remove custom events that you've set.
    • eventString Name of event.
    • functionFunction (optional) The listener to remove.
  • unload: Unload and destroy a Howl object. This will immediately stop all play instances attached to this sound and remove it from the cache.

Global Methods  
The following methods are used to modify all sounds globally, and are called from the  Howler  object. 
  • mute: Mutes all sounds.
  • unmute: Unmutes all sounds and restores them to their previous volume.
  • volume: Get/set the global volume for all sounds.
    • volumeNumber (optional) Volume from 0.0 to 1.0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现两个音频文件同步播放,可以使用Howler.js的时间同步功能。以下是一个基本的示例代码: ```js // 创建两个音频实例 var sound1 = new Howl({ src: ['audio1.mp3'] }); var sound2 = new Howl({ src: ['audio2.mp3'] }); // 监听第一个音频实例的"play"事件 sound1.on('play', function() { // 获取第一个音频实例的当前播放时间 var startTime = sound1.seek() || 0; // 同步第二个音频实例的播放时间 sound2.seek(startTime); }); // 同时播放两个音频文件 sound1.play(); sound2.play(); ``` 在上面的代码中,我们首先创建了两个音频实例`sound1`和`sound2`,并为它们分别指定了音频文件的路径。然后,我们监听了`sound1`的`play`事件,获取了当前播放时间,并将其同步到`sound2`中,从而实现了两个音频文件的同步播放。 如果你想在同步播放时添加其他效果,比如交叉淡入淡出效果,你可以在`play`事件中使用Howler.js的`fade`方法来实现,例如: ```js // 监听第一个音频实例的"play"事件 sound1.on('play', function() { // 获取第一个音频实例的当前播放时间 var startTime = sound1.seek() || 0; // 同步第二个音频实例的播放时间,并添加交叉淡入淡出效果 sound2.seek(startTime); sound2.fade(0, 1, 1000); // 从0淡入到1,持续时间为1秒 }); // 同时播放两个音频文件,并添加交叉淡入淡出效果 sound1.play(); sound1.fade(1, 0, 1000); // 从1淡出到0,持续时间为1秒 sound2.play(); ``` 上面的代码中,我们在`play`事件中使用了`fade`方法来实现了交叉淡入淡出效果,使得两个音频文件在切换时更加平滑。同时,我们在两个音频实例的`play`方法中也添加了交叉淡入淡出效果,使得播放开始时也更加平滑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值