I'm trying to determine whether the browser supports autoplay on load.
I'm using the following code, and it works fine on Android chrome, but for Desktop Chrome none of the lines in .catch or .then get executed. The promise seems to just return Pending promises ad infinitum.
Is this an actual Chrome bug or am I not understanding how Promises work here?
const promise = document.createElement('video').play();
if (promise instanceof Promise) {
promise.catch((error) => {
// Check if it is the right error
if (error.name === 'NotAllowedError') {
autoPlayAllowed = false;
} else {
throw error;
}
}).then(() => {
if (autoPlayAllowed) {
// Autoplay is allowed - continue with initialization
console.log('autoplay allowed')
} else {
// Autoplay is not allowed - wait for the user to trigger the play button manually
console.log('autoplay NOT allowed')
}
});
} else {
// Unknown if allowed
console.log('autoplay unknown')
}
Thanks!