Tuesday, June 8, 2021

React JS play sound

npm install --save uifx


import UIfx from 'uifx'

import bellAudio from './my-sounds/bell.mp3'

 

const bell = new UIfx(

  bellAudio,

  {

    volume: 0.4, // number between 0.0 ~ 1.0

    throttleMs: 100

  }

)

 

// playback

bell.play()

 

// temporarily change volume

bell.play(0.25)   // plays at 0.25 volume

bell.play()       // reverts to 0.4 volume

 

// set volume

bell.setVolume(0.5)

bell.play()       // plays at 0.5 volume

 

// ...also chainable!

bell.setVolume(0.5).play()


It uses the HTMLAudioElement API so it's available for all major desktop/mobile browsers. It also preloads audio files so sounds are immediately ready for playback.




references:

https://www.npmjs.com/package/uifx

No comments:

Post a Comment