@paradoxepoch/web-audio-player

1.0.0 • Public • Published

🎵 web-audio-player

A HTMLAudioElement interface wrapper that adds a few convenience methods and jQuery-like event listener management.

🛠️ Installation

npm install @paradoxepoch/web-audio-player

📖 Initialisation

// Import Library
import AudioPlayer from '@paradoxepoch/web-audio-player';

// Create a new audio instance (optionally accepts a source URL)
const audio = new AudioPlayer(src);

🔊 Playback Controls

// Change audio source
audio.src = 'http://example.com/audio.mp3';

/**
* Set audio volume with a swing fade.
* - Accepts new volume (0-100), fade duration (ms) and an interval (ms).
* - Only the new volume param is required. Fade duration and interval are optional and default to 1500ms and 13ms respectively.
* - Returns a promise that resolves when the fade completes.
*/
audio.setVolume(newVolume, fadeMs, intervalMs);

// Play audio
audio.play();

// Pause audio
audio.pause();

// Stop audio (resets position to 0)
audio.stop();

// Toggle audio playback (between play & stop)
audio.toggle();

// Getter, returns bool of audio playback status
audio.isPlaying;

// Getter, return bool of whether audio will loop
audio.loop;

// Setter, sets whether audio will loop (bool)
audio.loop = true;

🎉 Event Handling

/**
* Adds an event listener to the AudioPlayer instance
* - Accepts any HTMLAudioElement event and a handler function.
* - Returns index of handler function in this._eventHandlers[event] array. Can be passed to this.off to remove the listener.
*/
audio.on(event, handlerFunction);

/**
* Removes all listeners associated with an event from the AudioPlayer instance,
* or optionally a specific listener if handlerIndex is specified
* - Accepts the event name and optionally the index of a specific listener to remove.
*   If no handlerIndex is provided, removes all listeners bound to the event.
*/
audio.off(event, handlerIndex);

Package Sidebar

Install

npm i @paradoxepoch/web-audio-player

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

10.1 kB

Total Files

5

Last publish

Collaborators

  • paradoxepoch