• Stars
    star
    2,614
  • Rank 17,511 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React Hook for playing sound effects

useSound

A React Hook for Sound Effects

The web needs more (tasteful) sounds!

  • 👂 Lets your website communicate using 2 human senses instead of 1
  • 🔥 Declarative Hooks API
  • ⚡️ <1kb bytes (gzip) in your bundle! ~10kb loaded async.
  • Built with Typescript
  • 🗣 Uses a powerful, battle-tested audio utility: Howler.js

Minified file size License: MIT NPM version Code of Conduct

This library only works with React DOM, but @remigallego created an alternative for React Native! Check out react-native-use-sound.


Status

This project is “semi-maintained” 😅

I don't have the bandwidth right now to look into edge-case issues or help troubleshoot, but I plan on keeping it up-to-date with major React releases, and fixing issues that are both serious and common.

If you have ideas for features, or run into strange quirks, I thoroughly recommend forking the project and making it your own! It might seem intimidating, but the source isn't as complex as many other NPM packages; I defer all the hard audio work to Howler). If you've been using React for a while and are comfortable with hooks, you should feel right at home with this package's code.


Installation

Package can be added using yarn:

yarn add use-sound

Or, use NPM:

npm install use-sound

UMD build available on unpkg.


Demo

The tutorial includes many demos, as well as instructions for finding and preparing sound effects. It's a great place to start.

You can also view the storybook, which includes lots of quick examples.


Examples

Play sound on click

import useSound from 'use-sound';

import boopSfx from '../../sounds/boop.mp3';

const BoopButton = () => {
  const [play] = useSound(boopSfx);

  return <button onClick={play}>Boop!</button>;
};

Playing on hover

This demo only plays the sound while hovering over an element. The sound pauses when the mouse leaves the element:

NOTE: Many browsers disable sounds until the user has clicked somewhere on the page. If you're not hearing anything with this example, try clicking anywhere and trying again.

import useSound from 'use-sound';

import fanfareSfx from '../../sounds/fanfare.mp3';

const FanfareButton = () => {
  const [play, { stop }] = useSound(fanfareSfx);

  return (
    <button onMouseEnter={() => play()} onMouseLeave={() => stop()}>
      <span role="img" aria-label="trumpet">
        🎺
      </span>
    </button>
  );
};

Increase pitch on every click

With the playbackRate option, you can change the speed/pitch of the sample. This example plays a sound and makes it 10% faster each time:

import useSound from 'use-sound';

import glugSfx from '../../sounds/glug.mp3';

export const RisingPitch = () => {
  const [playbackRate, setPlaybackRate] = React.useState(0.75);

  const [play] = useSound(glugSfx, {
    playbackRate,
    // `interrupt` ensures that if the sound starts again before it's
    // ended, it will truncate it. Otherwise, the sound can overlap.
    interrupt: true,
  });

  const handleClick = () => {
    setPlaybackRate(playbackRate + 0.1);
    play();
  };

  return (
    <Button onClick={handleClick}>
      <span role="img" aria-label="Person with lines near mouth">
        🗣
      </span>
    </Button>
  );
};

Usage Notes

Importing/sourcing audio files

useSound requires a path to an audio file, and it isn't obvious how to provide one in a React application.

Using create-react-app, you can "import" an MP3 file. It will resolve to a dynamically-generated path:

import someAudioFile from '../sounds/sound.mp3';

console.log(someAudioFile); // “/build/sounds/sound-abc123.mp3”

If you try to pull this trick in another React build system like Next.js, you may get an error like this:

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.

The problem is that Webpack (the bundler used under-the-hood to generate JS bundles) doesn't know how to process an MP3 file.

If you have access to the Webpack config, you can update it to use file-loader, which will create a dynamic, publicly-accessible path to the file.

Alternatively, most tools will give you a "public" (create-react-app, Next.js) or a "static" (Gatsby) folder. You can drop your audio files in there, and then use a string path.

The sound files you'll use with use-sound follow the same rules as other static assets like images or fonts. Follow the guides for your meta-framework of choice:

⚠️ Async sound paths? ⚠️ If the URL to your audio file is loaded asynchronously, you might run into some problems. This probably isn't the right package for that usecase.

No sounds immediately after load

For the user's sake, browsers don't allow websites to produce sound until the user has interacted with them (eg. by clicking on something). No sound will be produced until the user clicks, taps, or triggers something.

useSound takes advantage of this: because we know that sounds won't be needed immediately on-load, we can lazy-load a third-party dependency.

useSound will add about 1kb gzip to your bundle, and will asynchronously fetch an additional package after load, which clocks in around 9kb gzip.

If the user does happen to click with something that makes noise before this dependency has been loaded and fetched, it will be a no-op (everything will still work, but no sound effect will play). In my experience this is exceedingly rare.

Reactive configuration

Consider the following snippet of code:

const [playbackRate, setPlaybackRate] = React.useState(0.75);

const [play] = useSound('/path/to/sound', { playbackRate });

playbackRate doesn't just serve as an initial value for the sound effect. If playbackRate changes, the sound will immediately begin playing at a new rate. This is true for all options passed to the useSound hook.


API Documentation

The useSound hook takes two arguments:

  • A URL to the sound that it wil load
  • A config object (HookOptions)

It produces an array with two values:

  • A function you can call to trigger the sound
  • An object with additional data and controls (ExposedData)

When calling the function to play the sound, you can pass it a set of options (PlayOptions).

Let's go through each of these in turn.

HookOptions

When calling useSound, you can pass it a variety of options:

Name Value
volume number
playbackRate number
interrupt boolean
soundEnabled boolean
sprite SpriteMap
[delegated]
  • volume is a number from 0 to 1, where 1 is full volume and 0 is comletely muted.
  • playbackRate is a number from 0.5 to 4. It can be used to slow down or speed up the sample. Like a turntable, changes to speed also affect pitch.
  • interrupt specifies whether or not the sound should be able to "overlap" if the play function is called again before the sound has ended.
  • soundEnabled allows you to pass a value (typically from context or redux or something) to mute all sounds. Note that this can be overridden in the PlayOptions, see below
  • sprite allows you to use a single useSound hook for multiple sound effects. See “Sprites” below.

[delegated] refers to the fact that any additional argument you pass in HookOptions will be forwarded to the Howl constructor. See "Escape hatches" below for more information.

The play function

When calling the hook, you get back a play function as the first item in the tuple:

const [play] = useSound('/meow.mp3');
//      ^ What we're talking about

You can call this function without any arguments when you want to trigger the sound. You can also call it with a PlayOptions object:

Name Value
id string
forceSoundEnabled boolean
playbackRate number
  • id is used for sprite identification. See “Sprites” below.
  • forceSoundEnabled allows you to override the soundEnabled boolean passed to HookOptions. You generally never want to do this. The only exception I've found: triggering a sound on the "Mute" button.
  • playbackRate is another way you can set a new playback rate, same as in HookOptions. In general you should prefer to do it through HookOptions, this is an escape hatch.

ExposedData

The hook produces a tuple with 2 options, the play function and an ExposedData object:

const [play, exposedData] = useSound('/meow.mp3');
//                ^ What we're talking about
Name Value
stop function ((id?: string) => void)
pause function ((id?: string) => void)
duration number (or null)
sound Howl (or null)
  • stop is a function you can use to pre-emptively halt the sound.
  • pause is like stop, except it can be resumed from the same point. Unless you know you'll want to resume, you should use stop; pause hogs resources, since it expects to be resumed at some point.
  • duration is the length of the sample, in milliseconds. It will be null until the sample has been loaded. Note that for sprites, it's the length of the entire file.
  • sound is an escape hatch. It grants you access to the underlying Howl instance. See the Howler documentation to learn more about how to use it. Note that this will be null for the first few moments after the component mounts.

Advanced

Sprites

An audio sprite is a single audio file that holds multiple samples. Instead of loading many individual sounds, you can load a single file and slice it up into multiple sections which can be triggered independently.

There can be a performance benefit to this, since it's less parallel network requests, but it can also be worth doing this if a single component needs multiple samples. See the Drum Machine story for an example.

For sprites, we'll need to define a SpriteMap. It looks like this:

const spriteMap = {
  laser: [0, 300],
  explosion: [1000, 300],
  meow: [2000, 75],
};

SpriteMap is an object. The keys are the ids for individual sounds. The value is a tuple (array of fixed length) with 2 items:

  • The starting time of the sample, in milliseconds, counted from the very beginning of the sample
  • The length of the sample, in milliseconds.

This visualization might make it clearer:

Waveform visualization showing how each sprite occupies a chunk of time, and is labeled by its start time and duration

We can pass our SpriteMap as one of our HookOptions:

const [play] = useSound('/path/to/sprite.mp3', {
  sprite: {
    laser: [0, 300],
    explosion: [1000, 300],
    meow: [2000, 75],
  },
});

To play a specific sprite, we'll pass its id when calling the play function:

<button
  onClick={() => play({id: 'laser'})}
>

Escape hatches

Howler is a very powerful library, and we've only exposed a tiny slice of what it can do in useSound. We expose two escape hatches to give you more control.

First, any unrecognized option you pass to HookOptions will be delegated to Howl. You can see the full list of options in the Howler docs. Here's an example of how we can use onend to fire a function when our sound stops playing:

const [play] = useSound('/thing.mp3', {
  onend: () => {
    console.info('Sound ended!');
  },
});

If you need more control, you should be able to use the sound object directly, which is an instance of Howler.

For example: Howler exposes a fade method, which lets you fade a sound in or out. You can call this method directly on the sound object:

const Arcade = () => {
  const [play, { sound }] = useSound('/win-theme.mp3');

  return (
    <button
      onClick={() => {
        // You win! Fade in the victory theme
        sound.fade(0, 1, 1000);
      }}
    >
      Click to win
    </button>
  );
};

More Repositories

1

react-flip-move

Effortless animation between DOM changes (eg. list reordering) using the FLIP technique.
JavaScript
4,080
star
2

guppy

🐠A friendly application manager and task runner for React.js
JavaScript
3,268
star
3

waveforms

An interactive, explorable explanation about the peculiar magic of sound waves.
JavaScript
1,430
star
4

panther

Discover artists through an infinite node graph
JavaScript
919
star
5

new-component

⚛ ⚡ CLI utility for quickly creating new React components. ⚡ ⚛
JavaScript
699
star
6

redux-vcr

📼 Record and replay user sessions
JavaScript
585
star
7

key-and-pad

🎹 Fun experiment with the Web Audio API 🎶
JavaScript
361
star
8

Tello

🐣 A simple and delightful way to track and manage TV shows.
JavaScript
329
star
9

tinkersynth

An experimental art project. Create unique art through serendipitous discovery.
JavaScript
282
star
10

beatmapper

A 3D editor for creating Beat Saber maps
JavaScript
271
star
11

blog

OLD VERSION of the joshwcomeau.com blog. Kept for historical purposes.
JavaScript
236
star
12

dark-mode-minimal

JavaScript
170
star
13

react-retro-hit-counter

🆕 Go back in time with this 90s-style hit counter.
JavaScript
162
star
14

redux-sounds

Middleware for playing audio / sound effects using Howler.js
JavaScript
130
star
15

dream-css-tool

JavaScript
119
star
16

react-collection-helpers

A suite of composable utility components to manipulate collections.
JavaScript
106
star
17

redux-favicon

Redux middleware that displays colourful notification badges in the favicon area.
JavaScript
105
star
18

nice-index

Atom package to rename `index.js` files to their parent directory names
CoffeeScript
82
star
19

react-europe-talk-2018

JavaScript
64
star
20

fakebook

A front-end Facebook clone, built with React and Redux
JavaScript
52
star
21

talk-2019

Slides for my 2019 talk, "Saving the Web 16ms at a Time"
JavaScript
52
star
22

understanding-react

Daily exploration of the React source code
42
star
23

talon-commands

Python
38
star
24

return-null

My React Europe 2017 lightning talk
JavaScript
35
star
25

explorable-explanations-with-react

JavaScript
35
star
26

word_dojo

JavaScript
18
star
27

react-boston-2018

My ReactBoston 2018 talk, The Case for Whimsy (Extended mix)
JavaScript
16
star
28

netlify-serverless-demo

JavaScript
16
star
29

css-for-js-flow-layout

HTML
14
star
30

whimsical-mail-client

JavaScript
14
star
31

ColourMatch

Search by Colour. Find photos with matching palettes.
CSS
12
star
32

talk-2020-react-europe

JavaScript
10
star
33

react-europe-workshop-confetti

JavaScript
10
star
34

plot

Experiments in pen plotting and generative art
JavaScript
9
star
35

sandpack-bundler-beta

JavaScript
8
star
36

react-play-button

JavaScript
7
star
37

react-europe-workshop-travel-site

JavaScript
7
star
38

deployed-screensaver

JavaScript
7
star
39

Uncover

📚 Aggregate new releases from your favourite authors. Built with Vuejs and Node
Vue
6
star
40

redux-vcr-todomvc

ReduxVCR integrated into TodoMVC.
JavaScript
5
star
41

react-letter-animation

A take on Mike Bostock's General Update Pattern, using React Flip Move.
JavaScript
5
star
42

Perseus

Gather info about your stargazers. Uses the GitHub GraphQL API
JavaScript
5
star
43

gatsby-preview-demo

Gatsby starter for a Contentful project.
JavaScript
5
star
44

words-with-strangers-redux

A universal redux version of my Meteor attempt at Words with Friends (online scrabble).
JavaScript
4
star
45

leitner

Keep track of your position in the 64-day Leitner calendar
JavaScript
4
star
46

empowered-development-with-gatsby

My Gatsby Days LA 2020 talk!
HTML
4
star
47

react-floaters

Spring-based scroll animation experiment with React.js
JavaScript
4
star
48

datocms-Gatsby-Portfolio-Website-demo

CSS
4
star
49

tetris

A simple tetris clone, in React and Redux, using Redux Saga
JavaScript
4
star
50

katas

A bunch of CodeWars challenge solutions. Part of an ongoing blogging effort at https://medium.com/@joshuawcomeau
JavaScript
4
star
51

react-europe-workshop-twitter-like

JavaScript
4
star
52

joshbot

The Discord bot for my Course Platform's community.
JavaScript
3
star
53

unlikely-friends

Don't mind me. Experiments with Gatsby themes
JavaScript
3
star
54

dont_eat_here_toronto

A Chrome extension that displays Toronto DineSafe restaurant inspection stuff on Yelp restaurant pages.
JavaScript
3
star
55

script-search

Find code used on the world's top sites
Python
3
star
56

basilica

JavaScript
3
star
57

yger

🚀⚡️ Blazing fast blog built with Gatsby and Cosmic JS 🔥
JavaScript
3
star
58

gatsby-dark-mode

CSS
2
star
59

Mars-Rover-HTML

An HTML/CSS Mars Rover simulation
CSS
2
star
60

generic-portfolio

An example of a generic portfolio (what NOT to do)
HTML
2
star
61

ember-todo

Don't mind me! Just a toy app to familiarize myself with Ember
JavaScript
2
star
62

mono-gatsby-apps

CSS
2
star
63

Aracari

A simple-as-possible budgeting web app. Because I suck at budgeting.
JavaScript
2
star
64

temp-project-wordle

JavaScript
2
star
65

AngelHack_rando

1st Place @ AngelHack TO. Built in 24h.
Ruby
2
star
66

tree-shake-test

JavaScript
2
star
67

gatsby-personalization

CSS
2
star
68

ssr-repro

CSS
2
star
69

react-fluid-window-events

React component for smooth, efficient resize/scroll handling.
JavaScript
2
star
70

Percentext

a jQuery plugin that lets you style text elements by width.
JavaScript
2
star
71

RequestKittens

The only API ridiculous enough to let you find cats by emotion.
JavaScript
2
star
72

book-demo

Demo of Git fundamentals
2
star
73

HungryBelly

An extension of the winning 24-hour project created for AngelHackTO
Ruby
1
star
74

art

Generative art experiments
JavaScript
1
star
75

elevator-simulator

WIP
JavaScript
1
star
76

RAFT

Utility for efficient, organized window-level event handlers
JavaScript
1
star
77

CLYWmparison_blogembed

A Yoyo Comparison tool, used by Caribou Lodge Yoyo Works
JavaScript
1
star
78

TicTacToe

JavaScript
1
star
79

TeeVee

A simple Meteor app to help me keep track of which episodes of TV shows I've seen.
JavaScript
1
star
80

RequestKittensDocs

The documentation / sales site for the RequestKittens API
JavaScript
1
star
81

foodshow

A silly weekend project, using the Unsplash API to display a food slideshow.
JavaScript
1
star
82

egghead-optimized-images-1

HTML
1
star
83

react-simple-canvas

React components that replicate the SVG interface, but renders to an HTML5 Canvas
JavaScript
1
star
84

munsell-colors

JavaScript
1
star
85

joshwcc

My portfolio/blog. Nowhere close to done yet.
Ruby
1
star
86

egghead-optimized-images-2

HTML
1
star
87

learn-webgl

Experiments for education with WebGL. Don't mind me.
JavaScript
1
star
88

Crowdfunder

A Kickstarter clone. Bitmaker Labs final assignment.
Ruby
1
star
89

MEAN_stack_starter

A ready-to-go initialized MEAN stack with tons of customizations.
CSS
1
star
90

egghead-videos

JavaScript
1
star
91

pixelminer

An idle game (à la cookie clicker), built to help me experiment with flowtype.
JavaScript
1
star
92

huddle

A Meteor app that aims to help patients have better access to their medical files, and get second opinions from physicians on the platform.
CSS
1
star
93

joshwcc_ver2

Attempt #2 at the joshw.cc portfolio site.
Ruby
1
star
94

Some-new-project

1
star
95

Advent-of-Code-2016

JavaScript
1
star
96

confetti-temp

JavaScript
1
star
97

redux-server-persist

JavaScript
1
star
98

Tori

Twitter, but for haikus.
JavaScript
1
star
99

classroom-q

Gatsby experimentation
CSS
1
star
100

fntest

CSS
1
star