Home

Awesome

useSound - react-native-use-sound

🔊 A React Native Hook for playing sounds 🔊

react-native-use-sound is largely based on the work by @joshwcomeau use-sound

Installation

âš  You must first install react-native-sound âš 

npm i react-native-sound
cd ios && pod install

Then, our Hook can be added:

npm install react-native-use-sound

Examples

Basic Example

import useSound from "react-native-use-sound";
import { Button } from "react-native";

const MusicButton = () => {
  const coolMusic =
    "http://commondatastorage.googleapis.com/codeskulptor-demos/DDR_assets/Kangaroo_MusiQue_-_The_Neverwritten_Role_Playing_Game.mp3";
  const [play, pause, stop, data] = useSound(coolMusic);

  const handlePlay = () => {
    if (data.isPlaying) pause();
    else play();
  };

  return (
  <>
    <Button
      title={data.isPlaying ? "Pause" : "Play"}
      onPress={handlePlay} 
    />
    <Button
      title={"Stop"}
      onPress={stop} 
    />
  </>
  );
};

API Documentation

The useSound hook takes two arguments:

It produces an array with four values:

HookOptions

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

NameValue
volumenumber
interruptboolean
soundEnabledboolean
timeRatenumber
numberOfLoopsboolean

Data

The hook produces a tuple with 4 options, the play, pause, stop functions and an Data object:

const [play, pause, stop, data] = useSound("/meow.mp3");
//                        ^ What we're talking about
NameValue
soundSound
seekfunction ((sec: number) => void)
isPlayingboolean
durationnumber
currrentTimenumber
loadingboolean