C_Z_
C_Z_

Reputation: 7796

Audio in a chrome extension; sometimes works, sometimes results in blank DOMException

I am writing an extension for myself that plays a sound based on reading stuff from the DOM.

Sometimes the sound plays properly, sometimes it fails with

uncaught (in promise) DOMException

With no further information.

As I understand it, this is the type of issue that happens when you attempt to insert autoplay audio into a webpage without defaulting to muted. If that is the case, why is my extension working sometimes but not other times?

I can confirm that the DOM is parsed correctly, and the error happens in the following call to play:

let my_audio = new Audio(chrome.runtime.getURL("media/beep.wav"));
my_audio.play();

The media file is properly registered in the manifest.json

"web_accessible_resources":[
    "media/beep.wav"
],

Are there other causes to the uncaught (in promise) DOMException error?

Upvotes: 0

Views: 155

Answers (1)

Kaiido
Kaiido

Reputation: 136658

Certainly because you did interact with the page, which would leverage the autoplay policies in Chrome (beware it may not be enough in other browsers even if you may not target other browsers now).

Upvotes: 2

Related Questions