From 39ba9b81af9f2d374c206d3ef2fef64bb89b04bc Mon Sep 17 00:00:00 2001 From: Caitlin Hennessy Date: Thu, 4 Jan 2018 12:20:31 -0800 Subject: [PATCH] Initial work --- libs/music/sounds.ts | 4 +++- sim/state/sounds.ts | 15 +++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index c79caf18..a5f41c7d 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -266,8 +266,10 @@ namespace music { //% blockId=music_play_sound_effect_until_done block="play sound effect %sound|until done" //% weight=98 blockGap=8 export function playSoundEffectUntilDone(sound: Sound) { - if (!sound) return; + if (!sound || numSoundsPlaying >= soundsLimit) return; + numSoundsPlaying++; sound.play(); + numSoundsPlaying--; } /** diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index fd7771ed..54f500ba 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -10,8 +10,6 @@ namespace pxsim.music { } namespace pxsim.SoundMethods { - let numSoundsPlaying = 0; - const soundsLimit = 1; let audio: HTMLAudioElement; export function buffer(buf: RefBuffer) { @@ -27,18 +25,15 @@ namespace pxsim.SoundMethods { } export function play(buf: RefBuffer, volume: number) { - if (!buf || numSoundsPlaying >= soundsLimit) { + if (!buf) { return Promise.resolve(); } return new Promise(resolve => { let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) audio = new Audio(url) - audio.onended = () => { - resolve(); - numSoundsPlaying--; - } - numSoundsPlaying++; - audio.play() + audio.onended = () => resolve(); + audio.onpause = () => resolve(); + audio.play(); }) } @@ -46,8 +41,8 @@ namespace pxsim.SoundMethods { return new Promise(resolve => { if (audio) { audio.pause(); - numSoundsPlaying--; } + resolve(); }) }