playcanvas
Version:
PlayCanvas WebGL game engine
36 lines (34 loc) • 1.01 kB
JavaScript
/**
* Represents the raw audio data of playable sound. A Sound is the resource of an audio
* {@link Asset}. An audio asset can be assigned to a {@link SoundSlot} owned by a
* {@link SoundComponent}.
*
* @category Sound
*/ class Sound {
/**
* Create a new Sound instance.
*
* @param {HTMLAudioElement|AudioBuffer} resource - If the Web Audio API is supported, pass an
* AudioBuffer object, otherwise an Audio object.
*/ constructor(resource){
if (resource instanceof Audio) {
this.audio = resource;
} else {
this.buffer = resource;
}
}
/**
* Gets the duration of the sound. If the sound is not loaded it returns 0.
*
* @type {number}
*/ get duration() {
let duration = 0;
if (this.buffer) {
duration = this.buffer.duration;
} else if (this.audio) {
duration = this.audio.duration;
}
return duration || 0;
}
}
export { Sound };