KKitForma.

Language

EnglishEnglishTürkçeTurkishGuide unavailable in this language · open toolsDeutschGermanGuide unavailable in this language · open toolsEspañolSpanishGuide unavailable in this language · open toolsFrançaisFrenchGuide unavailable in this language · open toolsPortuguêsPortugueseGuide unavailable in this language · open toolsItalianoItalianGuide unavailable in this language · open toolsNederlandsDutchGuide unavailable in this language · open toolsPolskiPolishGuide unavailable in this language · open toolsРусскийRussianGuide unavailable in this language · open toolsУкраїнськаUkrainianGuide unavailable in this language · open toolsSvenskaSwedishGuide unavailable in this language · open toolsNorskNorwegianGuide unavailable in this language · open toolsDanskDanishGuide unavailable in this language · open toolsSuomiFinnishGuide unavailable in this language · open toolsČeštinaCzechGuide unavailable in this language · open toolsRomânăRomanianGuide unavailable in this language · open toolsΕλληνικάGreekGuide unavailable in this language · open toolsالعربيةArabicGuide unavailable in this language · open toolsעבריתHebrewGuide unavailable in this language · open toolsفارسیPersianGuide unavailable in this language · open toolsاردوUrduGuide unavailable in this language · open toolsहिन्दीHindiGuide unavailable in this language · open toolsবাংলাBengaliGuide unavailable in this language · open toolsதமிழ்TamilGuide unavailable in this language · open toolsతెలుగుTeluguGuide unavailable in this language · open toolsमराठीMarathiGuide unavailable in this language · open toolsગુજરાતીGujaratiGuide unavailable in this language · open tools简体中文Chinese SimplifiedGuide unavailable in this language · open tools繁體中文Chinese TraditionalGuide unavailable in this language · open tools日本語JapaneseGuide unavailable in this language · open tools한국어KoreanGuide unavailable in this language · open toolsTiếng ViệtVietnameseGuide unavailable in this language · open toolsไทยThaiGuide unavailable in this language · open toolsBahasa IndonesiaIndonesianGuide unavailable in this language · open toolsBahasa MelayuMalayGuide unavailable in this language · open toolsFilipinoFilipinoGuide unavailable in this language · open toolsKiswahiliSwahiliGuide unavailable in this language · open toolsAfrikaansAfrikaansGuide unavailable in this language · open toolsMagyarHungarianGuide unavailable in this language · open toolsБългарскиBulgarianGuide unavailable in this language · open toolsHrvatskiCroatianGuide unavailable in this language · open toolsSrpskiSerbianGuide unavailable in this language · open toolsSlovenčinaSlovakGuide unavailable in this language · open toolsSlovenščinaSlovenianGuide unavailable in this language · open toolsLietuviųLithuanianGuide unavailable in this language · open toolsLatviešuLatvianGuide unavailable in this language · open toolsEestiEstonianGuide unavailable in this language · open toolsCatalàCatalanGuide unavailable in this language · open toolsEuskaraBasqueGuide unavailable in this language · open tools
← Questions & answers

KITFORMA · EDITORIAL ANSWER

Why does an autoplay failure produce an unhandled promise rejection?

KitForma editorial guide. play() returns a promise.

HTML, CSS & accessibility →

Step-by-step answer

play() returns a promise. Handle rejection and keep a keyboard-operable Play button available so a visitor can retry from a real user action. A rejection alone does not prove autoplay policy caused it; the format or media resource may also have failed. Save this as a local HTML page beside your own supported demo.mp4, then serve the directory over HTTP. Native video controls remain available. Add captions when the actual content needs them.
html
<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>
Test with audible media in a fresh browser session where autoplay is blocked: the page must show the blocked message, leave the Play button enabled and produce no unhandled rejection. Activate the button using the keyboard; a successful attempt should show Playing. Also test an unsupported file and muted playback. Browser policy varies, so autoplay may succeed initially. If policy or embedding permissions still deny playback, another click cannot guarantee success. Keep the status tied to the promise outcome and the media events.

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.

Discussion and community answers

Loading community…
KitForma

What would you like to do?