Initial work
This commit is contained in:
parent
52bdf94233
commit
39ba9b81af
@ -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--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user