KITFORMA · EDITORIAL ANSWER
Why does an autoplay failure produce an unhandled promise rejection?
KitForma editorial guide. play() returns a promise.
Step-by-step answer
<video id="demo" controls playsinline preload="metadata">
<source src="demo.mp4" type="video/mp4">
</video>
<button id="play" type="button">Play video</button>
<p id="playback-status" role="status" aria-live="polite">Ready.</p>
<script>
const video = document.querySelector("#demo");
const button = document.querySelector("#play");
const status = document.querySelector("#playback-status");
async function requestPlayback() {
button.disabled = true;
status.textContent = "Starting playback...";
try {
await video.play();
status.textContent = video.paused ? "Playback is paused." : "Playing.";
} catch (error) {
const name = error && error.name;
status.textContent = name === "NotAllowedError"
? "Playback was blocked. Use Play video or the native controls."
: name === "NotSupportedError"
? "This video could not be played. Check its file and format."
: "Playback failed. Check the media and try the controls.";
console.warn("Playback attempt failed:", name || "UnknownError");
} finally {
button.disabled = false;
}
}
button.addEventListener("click", requestPlayback);
video.addEventListener("pause", () => { status.textContent = "Paused."; });
video.addEventListener("ended", () => { status.textContent = "Finished."; });
// Optional first attempt: browsers may reject it without a user gesture.
requestPlayback();
</script>Sources and verification
Sources checked:
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.