@sn0wbit/use-sound
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

@sn0wbit/use-sound

Simple useSound hook that allow you to play audio file by just 1 hook. Using Howler

NPM JavaScript Style Guide

Install

npm install --save @sn0wbit/use-sound

Usage

import React, { useState } from 'react'
import useSound from '@sn0wbit/use-sound'

const navSounds = [
  require('./assets/audio/FRONTEND_NAV_GEN_03.wav'),
  require('./assets/audio/FRONTEND_NAV_GEN_04.wav')
]

const App = () => {
  const [randomIndex, setRandomIndex] = useState<number>(0)

  const [playNavigate] = useSound({
    src: navSounds[randomIndex],
    volume: 0.5
  })
  const [playNavigate2] = useSound({
    src: 'https://cdn.freesound.org/previews/178/178186_33044-lq.mp3',
    volume: 0.5
  })
  return (
    <div>
      <p>Example</p>
      <div>
      <button onClick={() => {
          randomIndex === 0 ? setRandomIndex(1) : setRandomIndex(0)
        }}>Switch sound 1</button>
      </div>
      <div>
        <button onClick={() => {
          playNavigate()
        }}>Play Navigate 1</button>
        <button onClick={() => {
          playNavigate2()
        }}>Play Navigate 2</button>
      </div>
    </div>
  )
}

License

MIT © Sn0wBiT

Readme

Keywords

none

Package Sidebar

Install

npm i @sn0wbit/use-sound

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

12.6 kB

Total Files

8

Last publish

Collaborators

  • sn0wbit