This is a small utility that preloads audio files and lets developers to play them at any point they need to. This utility properly cacthes all corresponding errors that may occur during loading and playback. It forces browser to preload files by appending HTML elements for each object that is initialized.
You can find an online demo here: https://lytrax.io/pub/dev/uiplay
NPM:
npm install uiplay
Yarn:
yarn add uiplay
Browser:
Download latest release from releases, or browse JSDeliver CDN to pick the latest version.
<script src='https://cdn.jsdelivr.net/npm/uiplay@latest/dist/uiplay.min.js'></script>
import UIPlay from 'uiplay';
const NofifyAudio = new UIPlay(
'orders_notify.mp3',
{
volume: 1
}
);
document.getElementById('play-audio').addEventListener('click', () => {
NofifyAudio.play();
});
UIPlay(file: string, options: object)
(contructor
)
The initialization of a new HTMLMediaElement object.
play(volume: number)
Plays the audio using a throtte timer. The volume
parameter can be omitted to use options
volume.
playImmediate(volume: number)
Plays the audio immediately without throtte. The volume
parameter can be omitted to use options
volume.
setVolume(volume: number)
Sets the volume level using decimal fraction (0-1).
NOTE: All methods return the actual object.
Option | Accepts | Description |
---|---|---|
volume |
Float fraction between 0-1 Default: 1 |
Controls the volume level for each audio object |
throttleMillis |
Integer of milliseconds Default: 300 |
Throttle time for the next play |
ignoreErrors |
Boolean Default: true |
Ignore or throw errors. Errors must be catched if throwning |