Answer the question
In order to leave comments, you need to log in
How to turn off sound on entire page in React?
Good evening.
Made a sound on the elements that plays on hover or click. Like this:
<li>
<Link onMouseOver={() => new Audio('/sound/menuhover.wav').play()} onClick={() => new Audio('/sound/openrouter.wav').play()} className={b({ block, elem: "item" })} href="/" target="_blank">
<span className={b({ block, elem: "support" })}></span>
<div className={b({ block, elem: "tooltip" })}>
<Translate>
{({ translate }) => <lang>{translate("navbar.support")}</lang>}
</Translate>
</div>
</Link>
</li>
<li>
{settings.isSound ? (
<Link className={b({ block, elem: "item" })} to="/faq">
<span className={b({ block, elem: "faq" })}></span>
<div className={b({ block, elem: "tooltip" })}>
<Translate>
{({ translate }) => <lang>{translate("navbar.faq")}</lang>}
</Translate>
</div>
</Link>
) : (
<Link onMouseOver={() => new Audio('/sound/menuhover.wav').play()} onClick={() => new Audio('/sound/openrouter.wav').play()} className={b({ block, elem: "item" })} to="/faq">
<span className={b({ block, elem: "faq" })}></span>
<div className={b({ block, elem: "tooltip" })}>
<Translate>
{({ translate }) => <lang>{translate("navbar.faq")}</lang>}
</Translate>
</div>
</Link>
)}
</li>
Answer the question
In order to leave comments, you need to log in
new Audio()
creates an HTMLAudioElement.
Instead of creating it every time, you can create it once or even insert it into the markup, and pull only play from the event.
To shut up the player, it needs to set the property muted
to true
. You don’t even leave a link to it here, so there is nowhere to put the property.
And how to do it globally is up to you to decide, you can use the redux store.
And by the way, it’s not clear what prevented you from doing just
onMouseOver={() => settings.isSound && new Audio('/sound/menuhover.wav').play()}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question