53 lines
956 B
JavaScript
53 lines
956 B
JavaScript
class Sound {
|
|
constructor(src, options = {}) {
|
|
this.playing = false;
|
|
this.src = src;
|
|
this.options = Object.assign({ volume: 1 }, options);
|
|
|
|
// Configure audio element
|
|
const audio = new Audio();
|
|
audio.src = src;
|
|
if (options.loop) {
|
|
audio.loop = true;
|
|
}
|
|
audio.addEventListener(
|
|
"error",
|
|
() => {
|
|
throw Error(`Error loading audio: ${src}`);
|
|
},
|
|
false
|
|
);
|
|
audio.addEventListener(
|
|
"ended",
|
|
() => {
|
|
this.playing = false;
|
|
},
|
|
false
|
|
);
|
|
this.audio = audio;
|
|
}
|
|
|
|
play(overrides) {
|
|
const { audio, options } = this;
|
|
const opts = Object.assign({ time: 0 }, options, overrides);
|
|
audio.volume = opts.volume;
|
|
audio.currentTime = opts.time;
|
|
audio.play();
|
|
this.playing = true;
|
|
}
|
|
|
|
stop() {
|
|
this.audio.pause();
|
|
this.playing = false;
|
|
}
|
|
|
|
get volume() {
|
|
return this.audio.volume;
|
|
}
|
|
|
|
set volume(volume) {
|
|
this.options.volume = this.audio.volume = volume;
|
|
}
|
|
}
|
|
|
|
module.exports = Sound; |