Initial work

This commit is contained in:
Caitlin Hennessy 2018-01-04 12:20:31 -08:00
parent 52bdf94233
commit 39ba9b81af
2 changed files with 8 additions and 11 deletions

View File

@ -266,8 +266,10 @@ namespace music {
//% blockId=music_play_sound_effect_until_done block="play sound effect %sound|until done" //% blockId=music_play_sound_effect_until_done block="play sound effect %sound|until done"
//% weight=98 blockGap=8 //% weight=98 blockGap=8
export function playSoundEffectUntilDone(sound: Sound) { export function playSoundEffectUntilDone(sound: Sound) {
if (!sound) return; if (!sound || numSoundsPlaying >= soundsLimit) return;
numSoundsPlaying++;
sound.play(); sound.play();
numSoundsPlaying--;
} }
/** /**

View File

@ -10,8 +10,6 @@ namespace pxsim.music {
} }
namespace pxsim.SoundMethods { namespace pxsim.SoundMethods {
let numSoundsPlaying = 0;
const soundsLimit = 1;
let audio: HTMLAudioElement; let audio: HTMLAudioElement;
export function buffer(buf: RefBuffer) { export function buffer(buf: RefBuffer) {
@ -27,18 +25,15 @@ namespace pxsim.SoundMethods {
} }
export function play(buf: RefBuffer, volume: number) { export function play(buf: RefBuffer, volume: number) {
if (!buf || numSoundsPlaying >= soundsLimit) { if (!buf) {
return Promise.resolve(); return Promise.resolve();
} }
return new Promise<void>(resolve => { return new Promise<void>(resolve => {
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
audio = new Audio(url) audio = new Audio(url)
audio.onended = () => { audio.onended = () => resolve();
resolve(); audio.onpause = () => resolve();
numSoundsPlaying--; audio.play();
}
numSoundsPlaying++;
audio.play()
}) })
} }
@ -46,8 +41,8 @@ namespace pxsim.SoundMethods {
return new Promise<void>(resolve => { return new Promise<void>(resolve => {
if (audio) { if (audio) {
audio.pause(); audio.pause();
numSoundsPlaying--;
} }
resolve();
}) })
} }