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"
//% weight=98 blockGap=8
export function playSoundEffectUntilDone(sound: Sound) {
if (!sound) return;
if (!sound || numSoundsPlaying >= soundsLimit) return;
numSoundsPlaying++;
sound.play();
numSoundsPlaying--;
}
/**

View File

@ -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<void>(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<void>(resolve => {
if (audio) {
audio.pause();
numSoundsPlaying--;
}
resolve();
})
}