From dee2b25d8ef596f263ad86f66c43b4148ab5aaea Mon Sep 17 00:00:00 2001 From: Bill Date: Mon, 7 Mar 2022 02:35:32 -0500 Subject: [PATCH] Sound fix for play() before interaction --- src/api/utils/PlaySound.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/utils/PlaySound.ts b/src/api/utils/PlaySound.ts index 2c4bc026..392e4d01 100644 --- a/src/api/utils/PlaySound.ts +++ b/src/api/utils/PlaySound.ts @@ -1,7 +1,25 @@ +import { MouseEventType, TouchEventType } from '@nitrots/nitro-renderer'; import { NitroSoundEvent } from '@nitrots/nitro-renderer/src/nitro/events/NitroSoundEvent'; import { DispatchMainEvent } from '../../hooks'; -export function PlaySound(sampleCode: string): void +let canPlaySound = false; + +export const PlaySound = (sampleCode: string) => { + if(!canPlaySound) return; + DispatchMainEvent(new NitroSoundEvent(NitroSoundEvent.PLAY_SOUND, sampleCode)); } + +const eventTypes = [ MouseEventType.MOUSE_CLICK, MouseEventType.MOUSE_MOVE, MouseEventType.MOUSE_MOVE, TouchEventType.TOUCH_MOVE, 'focus' ]; + +const startListening = () => +{ + const stopListening = () => eventTypes.forEach(type => window.removeEventListener(type, onEvent)); + + const onEvent = (event: Event) => ((canPlaySound = true) && stopListening()); + + eventTypes.forEach(type => window.addEventListener(type, onEvent)); +} + +startListening();