npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

react-native-use-sound

v1.2.2

Published

### πŸ”Š A React Native Hook for playing sounds πŸ”Š

Downloads

680

Readme

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:

  • 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 |

  • volume is a number from 0 to 1, where 1 is full volume and 0 is comletely muted.
  • 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
  • timeRate is the frequency (in milliseconds) at which the currentTime value will be updated. Default is 1000,
  • numberOfLoops specifies the number of times you want the sound repeated. Note that you can use -1 to 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 |

  • sound is an escape hatch. It grants you access to the underlying Sound instance.
  • seek is a function you can use to seek to a position in the sound.
  • isPlaying lets you know whether this sound is currently playing or not. When the sound reaches the end, or it's interrupted with stop or paused, this value will flip back to false. You can use this to show some UI only while the sound is playing.
  • duration is the length of the sample, in milliseconds.
  • currentTime is the current time of the sample, in milliseconds. You can chose the rate at which this is updated by specifying timeRate in the hook options (see above).
  • loading lets you know whether the current sample is loading.