Open In App

How to catch an audio DOMException caused by a file that cannot play ?

Last Updated : 29 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will try to understand the fact that How to catch an audio DOMException caused by a file that cannot be played.

When trying to play an audio file, a DOMException may be thrown if the file is not supported or cannot be played. For example, if the file is not in a supported format or is corrupted. It is important to handle these exceptions in order to provide a better user experience and prevent the script from crashing.

The Audio() constructor creates a new HTMLAudioElement object, which can be used to play audio files. The play() method is used to start playing the audio file. However, if the file is not in a supported format or is corrupted, a DOMException will be thrown.

 

Example: If we try to play a file that is not in a supported format:

var audio = new Audio("file.abc");
audio.play();

Output:

NotSupportedError: The given file is not of a supported file type or has a codec that is not supported.

There are a few different ways to catch an audio DOMException caused by a file that cannot play, but here are two examples: 

Example 1: Using a try-catch block

HTML




<html>
  
<head>
    <title>Catch Audio DOMException</title>
</head>
  
<body>
    <input type="file" id="audioFileInput" />
    <button onclick="playAudio()">Play</button>
    <script>
        function playAudio() {
            var file = document
                        .getElementById("audioFileInput").files[0];
            var audio = new Audio();
            audio.src = URL.createObjectURL(file);
  
            var fileType = file.type;
            if (audio.canPlayType(fileType) === "") {
                alert("This file cannot be played.");
            } else {
                try {
                    audio.play();
                } catch (e) {
                    if (e.name === "NotAllowedError" 
                        || e.name === "SecurityError") {
                        alert("This file cannot be played.");
                    } else {
                        throw e;
                    }
                }
            }
        }
    </script>
</body>
  
</html>


Output:

 

In the above example, to catch an audio DOMException caused by a file that cannot play, check if the browser supports the file format using the canPlayType() method, try to play the file and catch any exceptions. Check if the exception is a security or permission issue and show an alert to the user, otherwise re-throw it.

Example 2: Using onerror event listener

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Catch Audio DOMException</title>
</head>
  
<body>
    <input type="file" id="audioFileInput" />
    <button onclick="playAudio()">Play</button>
    <script>
        function playAudio() {
            var file = document
                        .getElementById("audioFileInput").files[0];
            var audio = new Audio();
            audio.src = URL.createObjectURL(file);
  
            audio.onerror = function () {
                alert("Cannot play audio file: " 
                      + this.error.message);
            }
            audio.play();
        }
    </script>
</body>
  
</html>
<html>


Output:

 

The code uses an input field, button, and an onerror event listener to play an audio file selected by the user, if an exception occurs while playing the audio file an alert box is displayed with the error message.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads