1

I am trying to add audio on load notification on chrome. My Js code below. it's working fine on firefox but sometimes on chrome it's not running m

error

Uncaught (in promise) DOMException

audio_notice = new Audio('http://www.sousound.com/music/healing/healing_01.mp3');
     audio_notice.autoplay = true;
    audio_notice.addEventListener('ended', function () {
        try {
            this.currentTime = 0;
            this.play();
        } catch (e) {
        }
    }, false);
    audio_notice.addEventListener('load', function () {
        try {
            this.play();
        } catch (e) {
        }
    }, true);

## LIve example ##

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67

1 Answers1

1

I think the event "load" is not correct, it's not in the list of compatible events.

You can use loadeddata instead

audio_notice.addEventListener('loadeddata', function () {
    try {
        this.play();
    } catch (e) {
    }
}, true);
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • i am getting this error for using ur code this.play(); Uncaught (in promise) DOMException – Lemon Kazi Mar 27 '19 at 10:38
  • I found this post on SO, I think it's helpfull https://stackoverflow.com/questions/40276718/how-to-handle-uncaught-in-promise-domexception-the-play-request-was-interru – R3tep Mar 27 '19 at 10:42
  • I tried here. https://jsfiddle.net/lemonkazi/ohg2y47b/ it sometimes playing sound sometimes not. may be it's chrome issue. Is there any other way to set autoplay – Lemon Kazi Apr 28 '19 at 04:09
  • @LemonKazi There is a couple of information [here](https://developers.google.com/web/updates/2017/09/autoplay-policy-changes) – R3tep Apr 29 '19 at 07:44