Add looping backing track
This commit is contained in:
parent
7adfb2f735
commit
6f66ec7b3e
@ -127,8 +127,6 @@ function absoluteDraw() {
|
||||
}
|
||||
|
||||
function onAssetsLoaded() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
setup(60);
|
||||
|
@ -22,7 +22,8 @@ let soundAssetMap = {
|
||||
"heartbeat": "./assets/sounds/heartbeat.mp3",
|
||||
"inhale": "./assets/sounds/breathing/inhale.mp3",
|
||||
"exhale": "./assets/sounds/breathing/exhale.mp3",
|
||||
"cough":"./assets/sounds/cough.mp3"
|
||||
"cough": "./assets/sounds/cough.mp3",
|
||||
"backingtrack": "./assets/sounds/backingtrack.wav"
|
||||
}
|
||||
|
||||
// All available sounds
|
||||
@ -37,7 +38,8 @@ let soundAssets = {
|
||||
heartbeat: new SoundSnippet("heartbeat", audioAssetType.sfx),
|
||||
inhale: new SoundSnippet("inhale", audioAssetType.sfx),
|
||||
exhale: new SoundSnippet("exhale", audioAssetType.sfx),
|
||||
cough: new SoundSnippet("cough", audioAssetType.sfx)
|
||||
cough: new SoundSnippet("cough", audioAssetType.sfx),
|
||||
backingtrack: new SoundSnippet("backingtrack", audioAssetType.bgm)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,7 +38,7 @@ class SoundSnippet {
|
||||
cache(callback) {
|
||||
|
||||
// Set the audio SRC
|
||||
this.audio = new Howl({src:[this.assetPath]});
|
||||
this.audio = new Howl({src:[this.assetPath], loop: (this.assetType == audioAssetType.bgm.bgm)});
|
||||
|
||||
// Create a callback for loading finished
|
||||
this.audio.once("load", () => {
|
||||
@ -72,6 +72,24 @@ class SoundSnippet {
|
||||
|
||||
}
|
||||
|
||||
playForever() {
|
||||
// If autoplay is disabled, we notify the console
|
||||
if (canPlayAudio()) {
|
||||
|
||||
console.log(`[SoundSnippet] Playing ${this.asset_name} forever`);
|
||||
|
||||
|
||||
// Play the snippet
|
||||
this.audio.play();
|
||||
|
||||
// Schedule a replay worker
|
||||
setInterval(() => { this.audio.play(); console.log(`[SoundSnippet] Replaying ${this.asset_name}`) }, this.getLengthSeconds() * 1000);
|
||||
|
||||
} else {
|
||||
console.warn("[SoundSnippet] Tried to play audio with autoplay disabled. The user must press the play button before you can play audio");
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
||||
// Stop the snippet
|
||||
@ -83,7 +101,7 @@ class SoundSnippet {
|
||||
* Get the sound length in seconds
|
||||
*/
|
||||
getLengthSeconds() {
|
||||
return this.audio.duration;
|
||||
return this.audio.duration();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,4 +117,5 @@ class SoundSnippet {
|
||||
getHash() {
|
||||
return this.asset_hash;
|
||||
}
|
||||
|
||||
}
|
@ -26,6 +26,9 @@ function handleMainScreen(){
|
||||
if (rectpoint({ x: 415, y: 200, w: 300, h: 50 }, { x: cursor.x, y: cursor.y }) && mouseDown[0]) {
|
||||
globalState = globalStates.levelTransition;
|
||||
timer = 0;
|
||||
|
||||
// Play the bgm
|
||||
soundAssets.backingtrack.playForever();
|
||||
}
|
||||
if (rectpoint({ x: 415, y: 550, w: 300, h: 50 }, { x: cursor.x, y: cursor.y }) && mouseDown[0]) {
|
||||
titleScreenState = "credits"
|
||||
|
BIN
docs/assets/sounds/backingtrack.wav
Normal file
BIN
docs/assets/sounds/backingtrack.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user