Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evandrocoan/f6f34b3c3e22b51f9345fea0cb51887c to your computer and use it in GitHub Desktop.
Save evandrocoan/f6f34b3c3e22b51f9345fea0cb51887c to your computer and use it in GitHub Desktop.
  1. https://stackoverflow.com/questions/13614803/how-to-check-if-html5-audio-has-reached-different-errors
  2. https://developer.mozilla.org/en-US/docs/Web/API/MediaError
  3. https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error
  4. https://stackoverflow.com/questions/29681907/check-if-sound-file-exists-javascript
media.addEventListener(
    "error",
    event => {
        let target = event.target;
        let message;
        switch (target.error.code) {
            case target.error.MEDIA_ERR_ABORTED:
                message = `You aborted the media playback.`;
                break;
            case target.error.MEDIA_ERR_NETWORK:
                message = `A network error caused the media download to fail.`;
                break;
            case target.error.MEDIA_ERR_DECODE:
                message = `The media playback was aborted due to a corruption problem or because the media used features your browser did not support.`;
                break;
            case target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
                message = `The media not be loaded, either because the server or network or because the format is not supported.`;
                break;
            default:
                message = `An unknown error occurred.`;
                break;
        }
        // console.log(`Could not play '${filename}' due to '${message}'...`);
    },
    { once: true }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment