Use audio sprites with useSound
mainAn audio sprite is a single audio file containing multiple sound samples. You can load one file and trigger specific sections by defining a sprite map in the HookOptions.
A SpriteMap is an object where keys are sound IDs and values are tuples [startTime, duration] in milliseconds.
Note: When using sprites, the playbackRate parameter is not reactive; only the initial value provided will be used.
const spriteMap = {
laser: [0, 300],
explosion: [1000, 300],
meow: [2000, 75],
};
const [play] = useSound('/path/to/sprite.mp3', {
sprite: spriteMap,
});
// To play a specific sprite:
<button onClick={() => play({ id: 'laser' })}>Play Laser</button>