Skip to content

Commit

Permalink
Allow for manually setting the preload attribute, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldzjap committed Aug 30, 2018
1 parent 90d5f10 commit 897e67e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ View the code for a full playlist example [here](/example/src/playlist.js)
| `container` | mixed | `null` | **Required**: CSS-selector or HTML-element for displaying the waveform. |
| `height` | integer | `128` | The height of the waveform in pixels. |
| `interact` | boolean | `true` | Enables/disables mouse interaction with the waveform view. This may be changed at any point after creation. |
| `preload` | string | `metadata` | The value of the preload attribute of the audio element. |
| `progressColor` | string | `#31708f` | The fill color of the waveform bars that have been played back so far. |
| `responsive` | boolean | `true` | If set to true, the width of the waveform view adapts to the width of the container element. |
| `useGradient` | boolean | `true` | Indicates if the waveform should be drawn with a gradient or not. |
Expand Down Expand Up @@ -121,6 +122,20 @@ View the code for a full playlist example [here](/example/src/playlist.js)
`url` is 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 `data` is 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.
* `loadAudio(url)`

Load an audio file from a URL without loading the waveform data and rendering the waveform.

**Arguments**:

`url` is a valid URL to an audio file.
* `loadWaveform(data)`

Load the waveform data from a URL to a JSON file or explicitly provided waveform data without loading the audio file.

**Arguments**:

`data` is either a string representing a valid URL to a JSON file containing the waveform data or an array or object containing waveform data.
* `interact` or `interact = bool`

Setter or getter for enabling/disabling mouse interaction with the waveform view.
Expand Down
23 changes: 20 additions & 3 deletions src/WavePlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ import {getJSON, isString, isObject} from './lib/index.js';

class WavePlayer {

/**
* The default options for a new instance.
*
* @var {Object}
*/
_defaultOptions = {
preload: 'metadata'
};

/**
* The options for this waveplayer instance.
*
* @var {Object}
*/
_options;

/**
* The mediator singleton that will be used to listen to events and fire
* actions in response.
Expand Down Expand Up @@ -67,7 +83,8 @@ class WavePlayer {
// Create a new mediator if there does not exist one yet
if (!WavePlayer._mediator) WavePlayer._mediator = new Mediator;

this._waveView = new WaveView(null, {...options});
this._options = {...this._defaultOptions, ...options};
this._waveView = new WaveView(null, {...this._options});

Promise.all([
this._initializeAudioElm(),
Expand Down Expand Up @@ -457,7 +474,7 @@ class WavePlayer {
const audioElm = document.createElement('audio');
audioElm.controls = false;
audioElm.autoplay = false;
audioElm.preload = 'auto';
audioElm.preload = this._options.preload;

return audioElm;
}
Expand All @@ -473,7 +490,7 @@ class WavePlayer {
getJSON(url)
.then(response => {
this._waveView.drawWave(
typeof response === 'object'
isObject(response)
? response[Object.keys(response)[0]]
: response,
0
Expand Down
2 changes: 1 addition & 1 deletion src/WaveView.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WaveView {
_data = null;

/**
* The options for this waveplayer instance.
* The options for this waveview instance.
*
* @var {Object}
*/
Expand Down

0 comments on commit 897e67e

Please sign in to comment.