howler.js is an audio library for the modern web. It defaults to Web Audio API and falls back to HTML5 Audio. This makes working with audio in JavaScript easy and reliable across all platforms.
Additional information, live demos and a user showcase are available at howlerjs.com.
Follow on Twitter for howler.js and development-related discussion: @GoldFireStudios.
Tested in the following browsers/versions:
Several options to get up and running:
git clone https://github.com/goldfire/howler.js.git
npm install howler
yarn add howler
bower install howler
cdnjs
jsDelivr
In the browser:
<script src="/path/to/howler.js"></script> <script> var sound = new Howl({ src: ['sound.webm', 'sound.mp3'] }); </script>
As a dependency:
import {Howl, Howler} from 'howler';
const {Howl, Howler} = require('howler');
Included distribution files:
howler.core
and howler.spatial
. It includes all functionality that howler comes with.howler.core
to operate as it is simply an add-on to the core.var sound = new Howl({ src: ['sound.mp3'] }); sound.play();
var sound = new Howl({ src: ['stream.mp3'], html5: true }); sound.play();
var sound = new Howl({ src: ['sound.webm', 'sound.mp3', 'sound.wav'], autoplay: true, loop: true, volume: 0.5, onend: function() { console.log('Finished!'); } });
var sound = new Howl({ src: ['sounds.webm', 'sounds.mp3'], sprite: { blast: [0, 3000], laser: [4000, 1000], winner: [6000, 5000] } }); // Shoot the laser! sound.play('laser');
var sound = new Howl({ src: ['sound.webm', 'sound.mp3'] }); // Clear listener after first call. sound.once('load', function(){ sound.play(); }); // Fires when the sound finishes playing. sound.on('end', function(){ console.log('Finished!'); });
var sound = new Howl({ src: ['sound.webm', 'sound.mp3'] }); // Play returns a unique Sound ID that can be passed // into any method on Howl to control that specific sound. var id1 = sound.play(); var id2 = sound.play(); // Fade out the first sound and speed up the second. sound.fade(1, 0, 1000, id1); sound.rate(1.5, id2);
import {Howl, Howler} from 'howler'; // Setup the new Howl. const sound = new Howl({ src: ['sound.webm', 'sound.mp3'] }); // Play the sound. sound.play(); // Change global volume. Howler.volume(0.5);
More in-depth examples (with accompanying live demos) can be found in the examples directory.
Array/String
[]
required
The sources to the track(s) to be loaded for the sound (URLs or base64 data URIs). These should be in order of preference, howler.js will automatically load the first one that is compatible with the current browser. If your files have no extensions, you will need to explicitly specify the extension using the format
property.
Number
1.0
The volume of the specific track, from 0.0
to 1.0
.
Boolean
false
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.
Boolean
false
Set to true
to automatically loop the sound forever.
Boolean|String
true
Automatically begin downloading the audio file when the Howl
is defined. If using HTML5 Audio, you can set this to 'metadata'
to only preload the file's metadata (to get its duration without download the entire file, for example).
Boolean
false
Set to true
to automatically start playback when sound is loaded.
Boolean
false
Set to true
to load the audio muted.
Object
{}
Define a sound sprite for the sound. The offset and duration are defined in milliseconds. A third (optional) parameter is available to set a sprite as looping. An easy way to generate compatible sound sprites is with audiosprite.
new Howl({ sprite: { key1: [offset, duration, (loop)] }, });
Number
1.0
The rate of playback. 0.5 to 4.0, with 1.0 being normal speed.
Number
5
The size of the inactive sounds pool. Once sounds are stopped or finish playing, they are marked as ended and ready for cleanup. We keep a pool of these to recycle for improved performance. Generally this doesn't need to be changed. It is important to keep in mind that when a sound is paused, it won't be removed from the pool and will still be considered active so that it can be resumed later.
Array
[]
howler.js automatically detects your file format from the extension, but you may also specify a format in situations where extraction won't work (such as with a SoundCloud stream).
Object
null
When using Web Audio, howler.js uses an XHR request to load the audio files. If you need to send custom headers, set the HTTP method or enable withCredentials
(see reference), include them with this parameter. Each is optional (method defaults to GET
, headers default to null
and withCredentials defaults to false
). For example:
// Using each of the properties. new Howl({ xhr: { method: 'POST', headers: { Authorization: 'Bearer:' + token, }, withCredentials: true, } }); // Only changing the method. new Howl({ xhr: { method: 'POST', } });
Function
Fires when the sound is loaded.
Function
Fires when the sound is unable to load. The first parameter is the ID of the sound (if it exists) and the second is the error message/code.
The load error codes are defined in the spec:
Function
Fires when the sound is unable to play. The first parameter is the ID of the sound and the second is the error message/code.
Function
Fires when the sound begins playing. The first parameter is the ID of the sound.
Function
Fires when the sound finishes playing (if it is looping, it'll fire at the end of each loop). The first parameter is the ID of the sound.
Function
Fires when the sound has been paused. The first parameter is the ID of the sound.
Function
Fires when the sound has been stopped. The first parameter is the ID of the sound.
Function
Fires when the sound has been muted/unmuted. The first parameter is the ID of the sound.
Function
Fires when the sound's volume has changed. The first parameter is the ID of the sound.
Function
Fires when the sound's playback rate has changed. The first parameter is the ID of the sound.
Function
Fires when the sound has been seeked. The first parameter is the ID of the sound.
Function
Fires when the current sound finishes fading in/out. The first parameter is the ID of the sound.
Function
Fires when audio has been automatically unlocked through a touch/click event.
Begins playback of a sound. Returns the sound id to be used with other methods. Only method that can't be chained.
String/Number
optional
Takes one parameter that can either be a sprite or sound ID. If a sprite is passed, a new sound will play based on the sprite's definition. If a sound ID is passed, the previously played sound will be played (for example, after pausing it). However, if an ID of a sound that has been drained from the pool is passed, nothing will play.Pauses playback of sound or group, saving the seek
of playback.
Number
optional
The sound ID. If none is passed, all sounds in group are paused.Stops playback of sound, resetting seek
to 0
.
Number
optional
The sound ID. If none is passed, all sounds in group are stopped.Mutes the sound, but doesn't pause the playback.
Boolean
optional
True to mute and false to unmute.Number
optional
The sound ID. If none is passed, all sounds in group are stopped.Get/set volume of this sound or the group. This method optionally takes 0, 1 or 2 arguments.
Number
optional
Volume from 0.0
to 1.0
.Number
optional
The sound ID. If none is passed, all sounds in group have volume altered relative to their own volume.Fade a currently playing sound between two volumes. Fires the fade
event when complete.
Number
Volume to fade from (0.0
to 1.0
).Number
Volume to fade to (0.0
to 1.0
).Number
Time in milliseconds to fade.Number
optional
The sound ID. If none is passed, all sounds in group will fade.Get/set the rate of playback for a sound. This method optionally takes 0, 1 or 2 arguments.
Number
optional
The rate of playback. 0.5 to 4.0, with 1.0 being normal speed.Number
optional
The sound ID. If none is passed, playback rate of all sounds in group will change.Get/set the position of playback for a sound. This method optionally takes 0, 1 or 2 arguments.
Number
optional
The position to move current playback to (in seconds).Number
optional
The sound ID. If none is passed, the first sound will seek.Get/set whether to loop the sound or group. This method can optionally take 0, 1 or 2 arguments.
Boolean
optional
To loop or not to loop, that is the question.Number
optional
The sound ID. If none is passed, all sounds in group will have their loop
property updated.Check the load status of the Howl
, returns a unloaded
, loading
or loaded
.
Check if a sound is currently playing or not, returns a Boolean
. If no sound ID is passed, check if any sound in the Howl
group is playing.
Number
optional
The sound ID to check.Get the duration of the audio source (in seconds). Will return 0 until after the load
event fires.
Number
optional
The sound ID to check. Passing an ID will return the duration of the sprite being played on this instance; otherwise, the full source duration is returned.Listen for events. Multiple events can be added by calling this multiple times.
String
Name of event to fire/set (load
, loaderror
, playerror
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
, unlock
).Function
Define function to fire on event.Number
optional
Only listen to events for this sound id.Same as on
, but it removes itself after the callback is fired.
String
Name of event to fire/set (load
, loaderror
, playerror
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
, unlock
).Function
Define function to fire on event.Number
optional
Only listen to events for this sound id.Remove event listener that you've set. Call without parameters to remove all events.
String
Name of event (load
, loaderror
, playerror
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
, unlock
).Function
optional
The listener to remove. Omit this to remove all events of type.Number
optional
Only remove events for this sound id.This is called by default, but if you set preload
to false, you must call load
before you can play any sounds.
Unload and destroy a Howl object. This will immediately stop all sounds attached to this sound and remove it from the cache.
Boolean
true
if the Web Audio API is
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物 制作、智能语音助手开发等。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。
专业的AI公文写作平台,公文写作神器
AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。
OpenAI Agents SDK,助力开发者便捷使用 OpenAI 相关功能。
openai-agents-python 是 OpenAI 推出的一款强大 Python SDK,它为开发者提供了与 OpenAI 模型交互的高效工具,支持工具调用、结果处理、追踪等功能,涵盖多种应用场景,如研究助手、财务研究等,能显著提升开发效率,让开发者更轻松地利用 OpenAI 的技术优势。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号