Av Prop Error
## HTML Audio/Video DOM error Property
The `error` property of the HTML Audio/Video DOM returns a `MediaError` object representing the most recent error state of an audio or video element. If no error has occurred, this property returns `null`.
By accessing the `code` property of the returned `MediaError` object, developers can programmatically determine why a media resource failed to load or play.
---
## Syntax
```javascript
mediaElement.error
// Or to get the specific error code:
mediaElement.error.code
```
### Return Value
The property returns a **`MediaError`** object (or `null` if there is no error). The `MediaError` object contains a `code` property which returns an integer representing the error state:
| Error Code | Constant | Description |
| :--- | :--- | :--- |
| **1** | `MEDIA_ERR_ABORTED` | The fetching process for the media resource was aborted by the user. |
| **2** | `MEDIA_ERR_NETWORK` | A network error occurred while downloading the media resource. |
| **3** | `MEDIA_ERR_DECODE` | An error occurred while decoding the media resource (corrupted file or unsupported encoding). |
| **4** | `MEDIA_ERR_SRC_NOT_SUPPORTED` | The media format is not supported, or the server/URL could not be reached. |
---
## Code Examples
### Example 1: Basic Error Detection
The following example demonstrates how to retrieve the error code of a video element when an error occurs.
```javascript
// Get the video element
const myVideo = document.getElementById("video1");
// Check if an error exists and alert its code
if (myVideo.error) {
alert("Error Code: " + myVideo.error.code);
} else {
console.log("No media errors detected.");
}
```
### Example 2: Real-time Error Handling with Event Listeners
In production environments, it is best practice to listen for the `error` event on the media element to handle playback failures dynamically.
```html
```
---
## Browser Compatibility
Modern web browsers fully support the `error` property and the `MediaError` interface on HTML5 `
YouTip