use-audio

v4.7.0arrow_drop_down
v4.7.0
STATUS
Passing
DOWNLOADS
69
VISIBILITY
Public
PUBLISHED
6 years ago
SIZE
N/A
1 contributor
Install use-audio as a package?
Copied
npm i @bit/giladshoham.react-hooks.use.use-audio
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
chevron_left
chevron_right

useAudio

Creates <audio> element, tracks its state and exposes playback controls.

Usage

import {useAudio} from 'react-use';

const Demo = () => {
  const [audio, state, controls, ref] = useAudio({
    src: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3',
    autoPlay: true,
  });

  return (
    <div>
      {audio}
      <pre>{JSON.stringify(state, null, 2)}</pre>
      <button onClick={controls.pause}>Pause</button>
      <button onClick={controls.play}>Play</button>
      <br/>
      <button onClick={controls.mute}>Mute</button>
      <button onClick={controls.unmute}>Un-mute</button>
      <br/>
      <button onClick={() => controls.volume(.1)}>Volume: 10%</button>
      <button onClick={() => controls.volume(.5)}>Volume: 50%</button>
      <button onClick={() => controls.volume(1)}>Volume: 100%</button>
      <br/>
      <button onClick={() => controls.seek(state.time - 5)}>-5 sec</button>
      <button onClick={() => controls.seek(state.time + 5)}>+5 sec</button>
    </div>
  );
};

Reference

const [audio, state, controls, ref] = useAudio(props);
const [audio, state, controls, ref] = useAudio(<audio {...props}/>);

audio is React’s <audio> element that you have to insert somewhere in your render tree, for example:

<div>{audio}</div>

state tracks the state of the audio and has the following shape:

{
  "buffered": [
    {
      "start": 0,
      "end": 425.952625
    }
  ],
  "time": 5.244996,
  "duration": 425.952625,
  "isPlaying": false,
  "muted": false,
  "volume": 1
}

controls is a list collection of methods that allow you to control the playback of the audio, it has the following interface:

interface AudioControls {
  play: () => Promise<void> | void;
  pause: () => void;
  mute: () => void;
  unmute: () => void;
  volume: (volume: number) => void;
  seek: (time: number) => void;
}

ref is a React reference to HTML <audio> element, you can access the element by ref.current, note that it may be null.

And finally, props — all props that <audio> accepts.

Help and resources