Add 'stopAllSounds' block

This commit is contained in:
Caitlin Hennessy
2017-12-13 16:31:42 -08:00
parent 14f57f54bf
commit 2d81be3b24
5 changed files with 35 additions and 1 deletions

View File

@ -3,11 +3,16 @@ namespace pxsim.music {
export function fromWAV(buf: RefBuffer) {
return incr(buf)
}
export function stopSounds() {
SoundMethods.stop()
}
}
namespace pxsim.SoundMethods {
let numSoundsPlaying = 0;
const soundsLimit = 1;
let audio: HTMLAudioElement;
export function buffer(buf: RefBuffer) {
return incr(buf)
@ -27,7 +32,7 @@ namespace pxsim.SoundMethods {
}
return new Promise<void>(resolve => {
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
let audio = new Audio(url)
audio = new Audio(url)
audio.onended = () => {
resolve();
numSoundsPlaying--;
@ -36,5 +41,10 @@ namespace pxsim.SoundMethods {
audio.play()
})
}
export function stop() {
audio.pause();
}
}