Home > Net >  How to add a sound on click to a reactjs component
How to add a sound on click to a reactjs component

Time:09-27

I'm working on a Realtime Chat Application on ReactJs and I just need to add a sound whenever there's an incoming message. Can somebody help me out? Any help is greatly appreciated

CodePudding user response:

Is your problem that you don't know how to play audio with Javascript?

If so, all you need is to import the file and create an audio object like this
const audio = new Audio(importedFile);. Now any time you add a new message just do this audio.play().

Note: The audio file should be inside src so that you can import it.

Look to my example for a better understanding. https://codesandbox.io/s/romantic-hermann-3fmpr?file=/src/App.js

  • Related