0

I'm using ReactJs. When my web app loads, I want it to play a sound. But that's not possible, because apparently, the browser wants the user to interact with the DOM first before you can do that. Is what I'm trying to do even possible, and if yes, how?

This is the thing I've tried so far, it doesn't work

export default function App(props) {
  const container = useRef();
  useEffect(() => {
    let click = new Event('click');
    container.current.dispatchEvent(click);
  }, []);
  return (
    <div ref={ container }></div>
  );
}
Nike Lepz
  • 485
  • 2
  • 12
  • 3
    Well…no. Nowadays browser get really serious on security and UX. Basically they mitigate the scenario like crazy popup ads and auto playing video in bg kind of thing you saw in early web era. So no way to get around it. – hackape Apr 21 '21 at 00:59
  • okay thanks. I don't have to spend hours trying to find the solution now that I now it's not possible. – Nike Lepz Apr 21 '21 at 01:05
  • @hackape but now that I think of it, facebook does do that, and yea I still use facebook :(. You open facebook. You haven't touched anything in the DOM. Your friend messages you. You get a 'ti-ting' notification sound. How does it do that? – Nike Lepz Apr 21 '21 at 01:07
  • You probably have authorized the fb website to do that before. – hackape Apr 21 '21 at 01:14
  • If you wanna test, you can go to setting and reset all authorization for facebook. You’re likely to get a notice “website facebook wanna send notifications…blah” shown by browser native widget. – hackape Apr 21 '21 at 01:16
  • ok. I'll do that:) – Nike Lepz Apr 21 '21 at 01:18
  • I used to think `autoplay` could work but [w3school](https://www.w3schools.com/tags/att_audio_autoplay.asp) suggest chrome does not allow it. – hackape Apr 21 '21 at 01:21
  • Yeah don’t take my word for it, I could be wrong. Tell me about your find out! – hackape Apr 21 '21 at 01:22
  • https://stackoverflow.com/questions/14356956/playing-audio-after-the-page-loads-in-html don't answers like these work? Also, cool name, Nike! – Kashish Arora Apr 21 '21 at 05:48

0 Answers0