Author: Michael Dzjaparidze
Browser Support & Other Requirements
waveplayer.js is tested to work on Chrome, Firefox and Opera (no support for Internet Explorer yet).
In order to minimise waveform drawing times it is necessary to supply an URL to a JSON file or a data array / object representing the waveform of the audio file numerically. When supplying a JSON file, it should have the same name as the corresponding audio file and should exist at the same location. An error will be thrown if this JSON file does not exist and no waveform data was passed in explicitly as a second argument to the
There exist a number of tools for extracting waveform data in JSON format from an audio file; wav2json or py-wav2json can be used for instance. It is enough to provide a single array of floating point values. If the JSON contains an associative array, only the first entree will be used.
This package is available through npm:
npm install --save waveplayer
The package is also still available through bower, although this will be faded out eventually
bower install waveplayer.js
After that the package may be directly included using a
or may be imported (ES6) in your own scripts files:
Load an audio file and play it
Create a waveplayer.js instance and pass in some (optional) options:
;var wavePlayer =container: '#waveform'barWidth: 4barGap: 1height: 128;
Load an audio file from a URL and start playback when loading has finished:
Schedule a playlist and play it through from start to finish
Load some audio files from a URL and start playback when loading has finished:
View the code for a full playlist example here
||Gap between bars in pixels.|
||Width of a bar in pixels.|
||Required: CSS-selector or HTML-element for displaying the waveform.|
||The height of the waveform in pixels.|
||Enables/disables mouse interaction with the waveform view. This may be changed at any point after creation.|
||The value of the preload attribute of the audio element.|
||The fill color of the waveform bars that have been played back so far.|
||If set to true, the width of the waveform view adapts to the width of the container element.|
||Indicates if the waveform should be drawn with a gradient or not.|
||The fill color of the waveform bars that have not been played back so far.|
||The width of the waveform in pixels (only relevant when the
Cancels the currently active playlist (will also stop the audio instantly).
Creates a new playlist (it will cancel the currently active playlist if there is one).
urlsis an array of valid audio file URL's or an array of objects each having an
urlpoints to the audio file URL and
datais an array or object holding valid waveform data (also see documentation for
options(optional) is an object with the following keys:
autoPlay: A boolean that indicates if the playlist should start playing automatically after it was created.
Getter for the playback time (in seconds) of the currently loaded / playing track.
Stops playback, cancels any running playlists and unsubscribes from any events that are listened for.
Getter for the total duration (in seconds) of the currently loaded / playing track.
Load an audio file from a URL and return a Promise which may be used to perform an action when the audio file has finished loading.
urlis a valid URL to an audio file.
data(optional) is an array or object containing waveform data. If it is not supplied, the waveform data is extracted from the JSON file. If
datais an object it is expected that the first key points to an array of waveform values. Note that only the first key found in the object is queried for waveform data.
Load an audio file from a URL without loading the waveform data and rendering the waveform.
urlis a valid URL to an audio file.
Load the waveform data from a URL to a JSON file or explicitly provided waveform data without loading the audio file.
datais either a string representing a valid URL to a JSON file containing the waveform data or an array or object containing waveform data.
interact = bool
Setter or getter for enabling/disabling mouse interaction with the waveform view.
boolis a boolean value for determining whether mouse interaction with the waveform view should be enabled or disabled.
Returns a boolean indicating whether audio is currently playing or not.
Subscribe to an event with a custom event handler.
topicis a string denoting a valid event name to which to subscribe.
fn()is the callback that will be evaluated every time the event is fired.
Pause the currently playing audio file.
Start playback of currently loaded audio file.
responsive = bool
Setter or getter for enabling/disabling responsive mode for the waveform view. If set, the width of the waveform view will adapt to the width of its parent container element.
boolis a boolean value for determining whether to switch between a responsive or fixed design of the waveform view.
Set the current time in seconds from which to playback audio.
secondsis a float value denoting the current time from which to playback the audio file.
Unsubscribe a particular callback or from the whole event all together.
topicis a string denoting a valid event name from which to unsubscribe.
fn()(optional) is a callback from which to unsubscribe.
volume = value
Set or get the audio playback volume, range:
[0 - 1].
valuesets the audio playback volume to this value.
waveplayer:canplay- Identical to the corresponding Media event. Fired when enough data is available so that the media can be played, at least for a couple of frames. Callback will receive the waveplayer instance.
waveplayer:initialized- Fired when the waveform view has been constructed and the HTML audio element has been created. Callback will receive the waveplayer instance.
waveplayer:skipped- Fired when the audio file was skipped forward or backward in time. Callback will receive the waveplayer instance and the new position of the playback header (in seconds).
waveplayer:playlist:finished- Fired when the playlist has finished playing all the queued audio. Callback will receive the waveplayer instance.
waveplayer:playlist:next- Fired when the next track in the playlist has been loaded and is about to be played. Callback will receive the waveplayer instance and an object with track information.
waveplayer:playlist:ready- Fired when the the first audio file in the playlist has finished loading. Callback will receive the waveplayer instance.