![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Howler.js is a JavaScript audio library for the modern web. It provides a simple API for playing and controlling audio files, supporting a wide range of audio formats and offering features like spatial audio, audio sprites, and more.
Basic Audio Playback
This feature allows you to play audio files with a simple API. You can specify the source of the audio file and control playback with methods like play, pause, and stop.
const sound = new Howl({
src: ['sound.mp3']
});
sound.play();
Audio Sprites
Audio sprites allow you to combine multiple audio files into a single file and play specific sections of it. This can be useful for reducing the number of HTTP requests and managing audio assets more efficiently.
const sound = new Howl({
src: ['soundsprite.mp3'],
sprite: {
blast: [0, 1000],
laser: [2000, 3000]
}
});
sound.play('blast');
Spatial Audio
Spatial audio allows you to position audio in 3D space, providing a more immersive experience. You can set the position of the sound source and control its volume and other properties.
const sound = new Howl({
src: ['sound.mp3'],
pos: [0, 0, -0.5],
volume: 1.0
});
sound.play();
Event Handling
Howler.js provides an event system that allows you to listen for various events such as load, play, pause, and stop. This can be useful for synchronizing audio with other parts of your application.
const sound = new Howl({
src: ['sound.mp3']
});
sound.on('load', function() {
console.log('Sound loaded!');
});
sound.play();
Tone.js is a framework for creating interactive music in the browser. It provides a wide range of features for audio synthesis, effects, and scheduling. Compared to Howler.js, Tone.js is more focused on music creation and manipulation, offering more advanced features for audio synthesis and effects.
Pizzicato.js is a library that aims to simplify the way you create and manipulate sounds with the Web Audio API. It provides a simple API for common audio tasks and includes features like effects and sound generation. While Howler.js is more focused on audio playback and control, Pizzicato.js offers more tools for sound creation and manipulation.
Wavesurfer.js is a customizable audio waveform visualization, built on top of the Web Audio API. It allows you to create interactive waveforms for audio files, providing features like regions, markers, and plugins. Compared to Howler.js, Wavesurfer.js is more focused on visualizing and interacting with audio waveforms.
howler.js is an audio library for the modern web. It defaults to Web Audio API and falls back to HTML5 Audio.
More documentation, examples and demos can be found at howlerjs.com.
Tested in the following browsers/versions:
Several options to get up and running:
git clone https://github.com/goldfire/howler.js.git
bower install howler
npm install howler
cdnjs
jsDelivr
var sound = new Howl({
src: ['sound.mp3']
});
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, 1000],
laser: [2000, 3000],
winner: [4000, 7500]
}
});
// shoot the laser!
sound.play('laser');
Array
[]
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 ext
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
true
Automatically begin downloading the audio file when the Howl
is defined.
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.
{
key: [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).
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.
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.
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
.
Number
The sound ID to check.Get the duration of the audio source. Will return 0 until after the load
event fires.
Number
optional
The sound ID to check. If none is passed, the first sound is checked.Listen for events. Multiple events can be added by calling this multiple times.
String
Name of event to fire/set (load
, loaderror
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
).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
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
).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
, play
, end
, pause
, stop
, mute
, volume
, rate
, seek
, fade
).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.
The following methods are used to modify all sounds globally, and are called from the Howler
object.
Mute or unmute all sounds.
Boolean
True to mute and false to unmute.Get/set the global volume for all sounds, relative to their own volume.
Number
optional
Volume from 0.0
to 1.0
.Check supported audio codecs. Returns true
if the codec is supported in the current browser.
String
File extension. One of: "mp3", "mpeg", opus", "ogg", "oga", "wav", "aac", "caf", m4a", "mp4", "weba", "webm", "dolby".Unload and destroy all currently loaded Howl objects. This will immediately stop all sounds and remove them from cache.
Boolean
true
if the Web Audio API is available.
Boolean
true
if any audio is available.
Boolean
true
Automatically attempts to enable audio on mobile (iOS, Android, etc) devices.
Boolean
true
Automatically suspends the Web Audio AudioContext after 30 seconds of inactivity to decrease processing and energy usage. Automatically resumes upon new playback. Set this property to false
to disable this behavior.
Boolean
Web Audio Only
Exposes the AudioContext
with Web Audio API.
Boolean
Web Audio Only
Exposes the master GainNode
with Web Audio API. This can be useful for writing plugins or advanced usage.
Get/set the 3D spatial position of the audio source for this sound or group. The most common usage is to set the x
position for left/right panning. Setting any value higher than 1.0
will begin to decrease the volume of the sound as it moves further away.
Number
The x-position of the audio from -1000.0
to 1000.0
.Number
The y-position of the audio from -1000.0
to 1000.0
.Number
The z-position of the audio from -1000.0
to 1000.0
.Number
optional
The sound ID. If none is passed, all in group will be updated.Get/set the direction the audio source is pointing in the 3D cartesian coordinate space. Depending on the direction of the sound, based on the cone
attributes, a sound pointing away from the listener can be quiet or silent.
Number
The x-orientation of the source.Number
The y-orientation of the source.Number
The z-orientation of the source.Number
optional
The sound ID. If none is passed, all in group will be updated.Get/set the velocity vector of the audio source or group. This controls both direction and speed in 3D space and is relative to the listener's velocity. The units are meters/second and are independent of position and orientation.
Number
The x-velocity of the source.Number
The y-velocity of the source.Number
The z-velocity of the source.Number
optional
The sound ID. If none is passed, all in group will be updated.Get/set the panner node's attributes for a sound or group of sounds. This method can optionall take 0, 1 or 2 arguments.
Object
All values to update.
360
There will be no volume reduction inside this angle.360
The volume will be reduced to a constant value of coneOuterGain
outside this angle.0
The amount of volume reduction outside of coneOuterAngle
.inverse
Determines algorithm to use to reduce volume as audio moves away from listener. Can be linear
, inverse
or `exponential.10000
Volume won't reduce between source/listener beyond this distance.HRTF
Determines which spatialization algorithm is used to position audio. Can be HRTF
or equalpower
.1
A reference distance for reducing volume as the source moves away from the listener.1
How quickly the volume reduces as source moves from listener.Number
optional
The sound ID. If none is passed, all in group will be updated.Array
[1, 0, 0]
Sets the direction the audio source is pointing in the 3D cartesian coordinate space. Depending on how direction the sound is, based on the cone
attributes, a sound pointing away from the listener can be quiet or silent.
Array
null
Sets the 3D spatial position of the audio source for this sound or group. The most common usage is to set the x
position for left/right panning. Setting any value higher than 1.0
will begin to decrease the volume of the sound as it moves further away.
Array
[0, 0, 0]
Sets the velocity vector of the audio source or group. This controls both direction and speed in 3D space and is relative to the listener's velocity. The units are meters/second and are independent of position and orientation.
Object
Sets the panner node's attributes for a sound or group of sounds. See the pannerAttr
method for all available options.
Function
Fires when the current sound has the listener position changes. The first parameter is the ID of the sound.
Function
Fires when the current sound has the direction of the listener changes. The first parameter is the ID of the sound.
Function
Fires when the current sound has the listener velocity changes. The first parameter is the ID of the sound.
Get/set the position of the listener in 3D cartesian space. Sounds using 3D position will be relative to the listener's position.
Number
The x-position of the listener.Number
The y-position of the listener.Number
The z-position of the listener.Get/set the direction the listener is pointing in the 3D cartesian space. A front and up vector must be provided. The front is the direction the face of the listener is pointing, and up is the direction the top of the listener is pointing. Thus, these values are expected to be at right angles from each other.
Number
The x-orientation of listener.Number
The y-orientation of listener.Number
The z-orientation of listener.Number
The x-orientation of the top of the listener.Number
The y-orientation of the top of the listener.Number
The z-orientation of the top of the listener.Get/set the velocity vector of the listener. This controls both direction and speed in 3D space, and is combined relative to a sound's velocity to determine how much doppler shift (pitch change) to apply.
Number
The x-velocity of listener.Number
The y-velocity of listener.Number
The z-velocity of listener.Get/set the audio listener attributes.
Object
All values to update.
1
Determines the amount of pitch shift from doppler effect.343.3
Speed of sound used to calculate doppler shift.By default, audio on iOS, Android, etc is locked until a sound is played within a user interaction, and then it plays normally the rest of the page session (Apple documentation). The default behavior of howler.js is to attempt to silently unlock audio playback by playing an empty buffer on the first touchend
event. This behavior can be disabled by calling:
Howler.mobileAutoEnable = false;
Full support for playback of the Dolby Audio format (currently support in Edge and Safari) is included. However, you must specify that the file you are loading is dolby
since it is in a mp4
container.
var dolbySound = new Howl({
src: ['sound.mp4', 'sound.webm', 'sound.mp3'],
format: ['dolby', 'webm', 'mp3']
});
Howler.js supports a wide array of audio codecs that have varying browser support ("mp3", "opus", "ogg", "wav", "aac", "m4a", "mp4", "webm", ...), but if you want full browser coverage you still need to use at least two of them. If your goal is to have the best balance of small filesize and high quality, based on extensive production testing, your best bet is to default to webm
and fallback to mp3
. webm
has nearly full browser coverage with a great combination of compression and quality. You'll need the mp3
fallback for Internet Explorer.
It is important to remember that howler.js selects the first compatible sound from your array of sources. So if you want webm
to be used before mp3
, you need to put the sources in that order.
If you want your webm
files to be seekable in Firefox, be sure to encode them with the cues element. One way to do this is by using the dash
flag in ffmpeg:
ffmpeg -i sound1.wav -dash 1 sound1.webm
Copyright (c) 2013-2016 James Simpson and GoldFire Studios, Inc.
Released under the MIT License.
FAQs
Javascript audio library for the modern web.
The npm package howler receives a total of 485,756 weekly downloads. As such, howler popularity was classified as popular.
We found that howler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.