From 55f4eaa1bb61a1b7e2a92a8e46ea5ce1e54fee4d Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Tue, 12 Apr 2022 16:48:10 +0500 Subject: [PATCH] w sounds --- package.json | 2 +- src/components/vp-explorer/VPExplorer.tsx | 4 ++++ src/lib/play-audio.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/lib/play-audio.ts diff --git a/package.json b/package.json index d91915e..d2db8e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lingdocs/pashto-inflector", - "version": "1.9.3", + "version": "1.9.4", "author": "lingdocs.com", "description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations", "homepage": "https://verbs.lingdocs.com", diff --git a/src/components/vp-explorer/VPExplorer.tsx b/src/components/vp-explorer/VPExplorer.tsx index f563d26..9c65f71 100644 --- a/src/components/vp-explorer/VPExplorer.tsx +++ b/src/components/vp-explorer/VPExplorer.tsx @@ -18,6 +18,7 @@ import InlinePs from "../InlinePs"; import { psStringEquals } from "../../lib/p-text-helpers"; import classNames from "classnames"; import { randFromArray } from "../../lib/misc-helpers"; +import playAudio from "../../lib/play-audio"; // import { useReward } from 'react-rewards'; const kingEmoji = "👑"; @@ -144,6 +145,8 @@ export function VPExplorer(props: { function checkQuizAnswer(a: T.PsString) { if (!quizState) return; if (isInAnswer(a, quizState.answer)) { + const toPlay = randFromArray([true, false, false]); + if (toPlay) playAudio(`correct-${randFromArray([1,2,3])}`); setShowCheck(true); setTimeout(() => { handleResetQuiz(); @@ -156,6 +159,7 @@ export function VPExplorer(props: { setCurrentCorrectEmoji(randFromArray(correctEmoji)); }, checkDuration * 2); } else { + playAudio(`wrong-${randFromArray([1,2])}`); navigator.vibrate(250); setQuizState({ ...quizState, diff --git a/src/lib/play-audio.ts b/src/lib/play-audio.ts new file mode 100644 index 0000000..68365a6 --- /dev/null +++ b/src/lib/play-audio.ts @@ -0,0 +1,11 @@ +export default function playAudio(a: string) { + if (!a) return; + let audio = new Audio(`https://verbs.lingdocs.com/sounds/${a}.mp3`); + audio.addEventListener("ended", () => { + audio.remove(); + audio.srcObject = null; + }); + audio.play().catch((e) => { + console.error(e); + }); +} \ No newline at end of file