move startup sound playing to welcome scene

This commit is contained in:
Bao Nguyen
2023-02-17 15:19:41 +07:00
parent 84984f5a95
commit 1e54a1f53d
2 changed files with 27 additions and 21 deletions

View File

@@ -53,7 +53,6 @@
];
let welcome_scene;
let fallback_timer;
onMount(async () => {
//load other pure js lib
@@ -65,26 +64,6 @@
]);
welcome_scene.self = welcome_scene;
fallback_timer = setTimeout(() => {
welcome_scene.destroy();
}, 7000)
let welcome_audio = new Audio("/audio/xp_startup.mp3");
welcome_audio.addEventListener("canplaythrough", (e) => {
console.log('canplaythrough');
if(welcome_scene){
welcome_audio.play().catch(async (e) => {
await utils.sleep(4000);
clearTimeout(fallback_timer);
welcome_scene.destroy();
});
}
});
welcome_audio.addEventListener("ended", (e) => {
console.log("xp_startup audio ended");
clearTimeout(fallback_timer);
welcome_scene.destroy();
});
});

View File

@@ -4,7 +4,34 @@
export let self;
let fallback_timer;
let destroyed = false;
onMount(() => {
let welcome_audio = new Audio("/audio/xp_startup.mp3");
welcome_audio.addEventListener("canplaythrough", (e) => {
console.log('canplaythrough');
if(!destroyed){
welcome_audio.play().catch(async (e) => {
});
}
});
welcome_audio.addEventListener("ended", (e) => {
console.log("xp_startup audio ended");
clearTimeout(fallback_timer);
self.destroy();
});
fallback_timer = setTimeout(() => {
self.destroy();
}, 7000)
})
export function destroy(){
destroyed = true;
self.$destroy();
}