---
title: Audio and Video Events
author: Henry Quackenboss
description: This article summarizes three common events in the DOM object that are used to manage audio and video files playing within webpages.
published: 2025-1-22
---

## Abort Event
Abort events are triggered when a video or audio file's loading is aborted due to another function or due to user intervention. The abort event does NOT trigger if a video or audio segment fails to load because of an error. Errors are instead checked by the onerror event. An abort event can be used to mitigate a situation where a user should have been able to access media but did not due to any situation besides an error.

### Three resources to learn more about abort events:

[onabort Event - w3 Schools](https://www.w3schools.com/jsref/event_onabort_media.asp)

[HTMLMediaElement: abort event - mdn web docs_](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/abort_event)

[The onabort event in Javascript - The JavaScript Source](https://javascriptsource.com/the-onabort-event-in-javascript/#:~:text=The%20JavaScript%20onabort%20event%20is,provide%20feedback%20to%20the%20user.)

### Abort event sample:
##### to try these code samples you will need an mp3 file named "trainingAudio.mp3"
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio and Video Events</title>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const trainingAudio=document.getElementById("trainingAudio");
            trainingAudio.onabort = () => {
                alert("Something went wrong! Please reload your webpage to continue listening to your mandatory education.");
                //I know I shouldn't use alert but go easy on me this is just a sample
            };
        });
    </script>
</head>
<body>
    <audio src="trainingAudio.mp3" controls id="trainingAudio"></audio>
</body>
</html>
```

## Playing Event
Playing events are triggered whenever an audio or video file begins or resumes playing. The playing event will also trigger once media begins playing after stopping to buffer due to insufficient data. The playing event can be used for a wide variety of purposes, including gathering data about how often media is started or freezes per user per instance. There could also be accessibility and user experience applications of knowing when a user is currently interacting with the media.
### Three resources to learn more about playing events:
[onplaying Event - w3 Schools](https://www.w3schools.com/jsref/event_onplaying.asp)

[HTMLMediaElement: playing event - mdn web docs_](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/playing_event)

[HTML DOM onplaying Event - geeks for geeks](https://www.geeksforgeeks.org/html-dom-onplaying-event/)

### Playing event sample:
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio and Video Events</title>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const trainingAudio=document.getElementById("trainingAudio");
            trainingAudio.onplaying = () => {
                alert("Enjoying the video? Remember to leave a 5 star review!");
            };
        });
    </script>
</head>
<body>
    <audio src="trainingAudio.mp3" controls id="trainingAudio"></audio>
</body>
</html>
```

## Ended Event
The ended event is triggered when a media file finishes playing. It is not triggered when a video is paused or freezes due to insufficient data. Ended events can also be used for a wide variety of purposes. One useful application that come to mind include to collect data about how many people finish watching or listening to media files or to check if a user has finished watching or listening to verify that they have. Another use that seems helpful is to display or play a thank you for watching/listening message at the end of media files.
### Three resources to learn more about ended events:
[onended Event - w3 Schools](https://www.w3schools.com/jsref/event_onended.asp)

[HTMLMediaElement: ended event - mdn web docs_](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/ended_event)

[HTML DOM onended Event - geeks for geeks](https://www.geeksforgeeks.org/html-dom-onended-event/)

### Ended event sample:

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio and Video Events</title>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const trainingAudio=document.getElementById("trainingAudio");
            trainingAudio.onended = () => {
                alert("Training complete! Please press next to continue.");
            };
        });
    </script>
</head>
<body>
    <audio src="trainingAudio.mp3" controls id="trainingAudio"></audio>
</body>
</html>
```