Google Chrome Uncaught (in promise) DOMException while playing AUDIO
You're receiving an uncaught exception because you aren't handling an error.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play
Audio is an HTMLMediaElement
object, and the play()
method returns a promise. Therefore I recommend handling the error.
var promise = audio.play();
if (promise) {
//Older browsers may not return a promise, according to the MDN website
promise.catch(function(error) { console.error(error); });
}
One of two errors are possible:
NotSupportedError
This means that the audio source is not supported by the browser (probably due to audio format)
NotAllowedError
This is the one I suspect you are triggering. This means the user needs to explicitly trigger the audio.play()
, so the method is only usable if it is in response to a user generated event such as a click event.
Docs:
The user agent (browser) or operating system doesn't allow playback of media in the current context or situation. This may happen, for example, if the browser requires the user to explicitly start media playback by clicking a "play" button.