1.2.2 ⢠Published 3 years ago
react-native-use-sound v1.2.2
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 installThen, our Hook can be added:
npm install react-native-use-soundExamples
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:
- A URL to the sound that it will load
- A config object (
HookOptions)
It produces an array with four values:
- A function you can call to play the sound
- A function you can call to pause the sound
- A function you can call to stop the sound
- An object with additional data and controls (
ExposedData)
HookOptions
When calling useSound, you can pass it a variety of options:
| Name | Value |
|---|---|
| volume | number |
| interrupt | boolean |
| soundEnabled | boolean |
| timeRate | number |
| numberOfLoops | boolean |
volumeis a number from0to1, where1is full volume and0is comletely muted.interruptspecifies whether or not the sound should be able to "overlap" if theplayfunction is called again before the sound has ended.soundEnabledallows you to pass a value (typically from context or redux or something) to mute all sounds. Note that this can be overridden in thePlayOptions, see belowtimeRateis the frequency (in milliseconds) at which thecurrentTimevalue will be updated. Default is 1000,numberOfLoopsspecifies the number of times you want the sound repeated. Note that you can use-1to Loop indefinitely until stop() is called.
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| Name | Value |
|---|---|
| sound | Sound |
| seek | function ((sec: number) => void) |
| isPlaying | boolean |
| duration | number |
| currrentTime | number |
| loading | boolean |
soundis an escape hatch. It grants you access to the underlyingSoundinstance.seekis a function you can use to seek to a position in the sound.isPlayinglets you know whether this sound is currently playing or not. When the sound reaches the end, or it's interrupted withstoporpaused, this value will flip back tofalse. You can use this to show some UI only while the sound is playing.durationis the length of the sample, in milliseconds.currentTimeis the current time of the sample, in milliseconds. You can chose the rate at which this is updated by specifyingtimeRatein the hook options (see above).loadinglets you know whether the current sample is loading.