@nent/core
Version:
79 lines (78 loc) • 2.25 kB
JavaScript
/*!
* NENT 2022
*/
import { DiscardStrategy } from './interfaces';
import { AudioLoader } from './list-loader';
import { PlayerBase } from './player-base';
import { markTrack } from './tracks';
/* It's a wrapper around an AudioLoader that keeps track of the currently playing track and allows you
to play a new track */
export class SoundPlayer 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();
}
/**
* It loads an audio file, and when it's done loading, it sets the active audio to null and calls the
* changed function
* @param {AudioInfo} info - AudioInfo
*/
load(info) {
this.loader.load(info, () => {
this.active = null;
this.changed();
});
this.changed();
}
/**
* It plays a track
* @param {string} trackId - The id of the track to play.
*/
async playTrack(trackId) {
const track = this.loader.findTrack(trackId);
if (track) {
this.loader.stop();
this.active = null;
this.discard(DiscardStrategy.next);
this.active = track;
track.play();
await markTrack(trackId);
this.changed();
}
}
/**
* If the active strategy is being discarded, destroy it and set it to null
* @param {DiscardStrategy[]} reasons - DiscardStrategy[]
*/
discard(...reasons) {
var _a;
if (this.active && reasons.includes(this.active.discard)) {
(_a = this.active) === null || _a === void 0 ? void 0 : _a.destroy();
this.active = null;
}
this.loader.discard(...reasons);
this.changed();
}
/**
* If the active item is not null, or the loader has items, then return true
* @returns A boolean value.
*/
hasAudio() {
return this.active != null || this.loader.hasItems();
}
/**
* It destroys the active scene and the loader.
*/
destroy() {
var _a;
(_a = this.active) === null || _a === void 0 ? void 0 : _a.destroy();
this.loader.destroy();
}
}