BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/lyminhnghia/use-sound

Sound Hook

lyminhnghia/use-sound
FreeHook

Custom React hook to load and play a sound from a given URL using the Web Audio API.

Install it

npx shadcn@latest add https://lyminhnghia.github.io/r/use-sound.json

Source

src/hooks/use-sound.tslyminhnghia.github.io/r/use-sound.json · first 6 KB
1"use client"
2
3import { useCallback, useEffect, useRef, useState } from "react"
4
5/**
6 * Cache storage for loaded audio buffers to prevent duplicate network requests and memory usage.
7 * Maps audio URL to its decoded AudioBuffer.
8 */
9const audioCache = new Map<
10 string,
11 {
12 buffer: AudioBuffer
13 loading: Promise<AudioBuffer>
14 } | null
15>()
16
17/**
18 * Shared AudioContext instance to avoid creating multiple contexts.
19 * Multiple AudioContexts can cause performance issues and resource exhaustion.
20 */
21let sharedAudioContext: AudioContext | null = null
22
23/**
24 * Gets or creates a shared AudioContext instance.
25 */
26function getAudioContext(): AudioContext | null {
27 if (sharedAudioContext) return sharedAudioContext
28
29 const AudioContextClass =
30 window.AudioContext ||
31 (window as unknown as { webkitAudioContext: typeof AudioContext })
32 .webkitAudioContext
33
34 if (!AudioContextClass) {
35 console.warn("Web Audio API is not supported in this browser.")
36 return null
37 }
38
39 sharedAudioContext = new AudioContextClass()
40 return sharedAudioContext
41}
42
43/**
44 * Custom React hook to load and play a sound from a given URL using the Web Audio API.
45 *
46 * This hook implements caching to prevent duplicate network requests and memory usage
47 * when the same audio file is used across multiple components.
48 *
49 * @param url - The URL of the audio file to load and play.
50 * @returns A function that, when called, plays the loaded sound.
51 *
52 * @remarks
53 * - Audio buffers are cached globally, so the same file is only loaded once
54 * - Uses a shared AudioContext to avoid resource exhaustion
55 * - If the Web Audio API is not supported in the browser, a warning is logged and playback is disabled
56 * - Errors during fetching or decoding the audio are logged to the console
57 *
58 * @example
59 * ```tsx
60 * const playClick = useSound('/sounds/click.mp3');
61 * // Later in an event handler:
62 * playClick();
63 * ```
64 */
65export function useSound(url: string) {
66 const audioCtxRef = useRef<AudioContext | null>(null)
67 const bufferRef = useRef<AudioBuffer | null>(null)
68
69 useEffect(() => {
70 const audioCtx = getAudioContext()
71 if (!audioCtx) return
72
73 audioCtxRef.current = audioCtx
74
75 // Check if already cached
76 const cached = audioCache.get(url)
77 if (cached?.buffer) {
78 bufferRef.current = cached.buffer
79 return
80 }
81
82 // Check if already loading
83 if (cached?.loading) {
84 cached.loading
85 .then((decoded) => {
86 bufferRef.current = decoded
87 })
88// … truncated

Files it writes

  • src/hooks/use-sound.ts

Facts

Registry
lyminhnghia
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
In the index
at or before 2026-08-11
Last confirmed
today

Go to the source

lyminhnghia.github.io lyminhnghia/lyminhnghia.github.io on GitHub Raw registry item This item as JSON

More from lyminhnghia

All 26 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Controllable State Hookuse-controllable-statelyminhnghiaHooksFreeno deps · 2 files

Also from Kynth Studios

Built for the same person as BlockDex

SkillWorks

Claude Code skills and plugins, scored

skillworks.kynth.studio

RuleStack

AGENTS.md gallery and agent-config comparison

rulestack.kynth.studio

ToolDrift

What the AI coding tools changed last night

tooldrift.kynth.studio

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex