UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

100 lines (99 loc) 3.04 kB
/*! * NENT 2022 */ import { DiscardStrategy } from './interfaces'; import { AudioLoader } from './list-loader'; import { AudioQueue } from './list-queue'; import { PlayerBase } from './player-base'; import { markTrack } from './tracks'; /* It's a class that manages the loading, playing, and queuing of audio */ export class MusicPlayer extends PlayerBase { /** * The constructor function is a special function that is called when an object is created from a * class * @param {Listener} changed - Listener - This is the listener that will be called when the value of * the property changes. */ constructor(changed) { super(); this.changed = changed; this.loader = new AudioLoader(); this.queue = new AudioQueue(); } /** * It queues an audio file, and if there is no active audio file, it plays the queued audio file * @param {AudioInfo} info - AudioInfo - The audio info of the audio you want to queue. */ queueAudio(info) { var _a; this.queue.queueAudio(info, () => { var _a; this.active = this.queue.getNext(); (_a = this.active) === null || _a === void 0 ? void 0 : _a.play(); this.changed(); }); if (this.active == null) { this.active = this.queue.getNext(); (_a = this.active) === null || _a === void 0 ? void 0 : _a.play(); } this.changed(); } /** * It loads an audio file, and then calls the changed function * @param {AudioInfo} info - AudioInfo */ load(info) { this.loader.load(info, this.changed); this.changed(); } /** * It plays a track * @param {string} trackId - The id of the track to play */ async playTrack(trackId) { var _a; const track = this.loader.findTrack(trackId); if (track) { this.loader.stop(); this.queue.insertTrack(track, () => { var _a; this.active = this.queue.getNext(); (_a = this.active) === null || _a === void 0 ? void 0 : _a.play(); this.changed(); }); this.discard(DiscardStrategy.next); this.active = this.queue.getNext(); (_a = this.active) === null || _a === void 0 ? void 0 : _a.play(); await markTrack(trackId); this.changed(); } } /** * If the loader is discarded, discard the queue and notify the world that the queue has changed. * @param {DiscardStrategy[]} reasons - DiscardStrategy[] */ discard(...reasons) { super.discard(...reasons); this.loader.discard(...reasons); this.queue.discard(...reasons); this.changed(); } /** * If the active audio is not null, or the loader has items, or the queue has items, then return true * @returns A boolean value. */ hasAudio() { return (this.active != null || this.loader.hasItems() || this.queue.hasItems()); } /** * It destroys the active scene, the loader, and the queue. */ destroy() { var _a; (_a = this.active) === null || _a === void 0 ? void 0 : _a.destroy(); this.loader.destroy(); this.queue.destroy(); } }