JSPM

@paradoxepoch/web-audio-player

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q18714F
  • License MIT

A straightforward audio player for browsers

Package Exports

  • @paradoxepoch/web-audio-player
  • @paradoxepoch/web-audio-player/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@paradoxepoch/web-audio-player) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

🎵 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);