Skip to content

Bug: audio.play() Promise rejection unhandled in static JS, autoplay failures are silent #99

Description

@anshul23102

Bug Report: Unhandled audio.play() Promise Rejection Causes Silent Playback Failure

Description

The music player JavaScript calls audio.play() to start playback. In all
modern browsers, audio.play() returns a Promise that is rejected when the
browser's autoplay policy blocks playback (which happens for any page loaded
without prior user interaction). Without a .catch() handler, the rejection
becomes an unhandled promise rejection, and the user sees no error message,
spinner state, or indication that playback failed.

Steps to Reproduce

  1. Open http://localhost:8000/ in Chrome or Firefox without any prior interaction.
  2. Click the play button.
  3. If autoplay is blocked, the play button appears to "work" but no audio plays.
  4. Check the browser console: Uncaught (in promise) DOMException: play() failed.

Root Cause

audio.play();  // Missing .catch()

audio.play() returns a Promise. Without .catch(), any rejection
(autoplay policy, unsupported codec, network error) is swallowed silently.

Impact

Users on browsers with strict autoplay policies (all major browsers by default)
receive no feedback when playback fails, making the app appear broken.

Proposed Fix

Handle the rejection and show user feedback:

const playPromise = audio.play();
if (playPromise !== undefined) {
    playPromise
        .then(() => { updatePlayButton(true); })
        .catch(err => {
            console.warn("Playback failed:", err.message);
            updatePlayButton(false);
            showToast("Click anywhere on the page to enable audio playback.");
        });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions