Sub topic 2
Pointing an Element at a Media File
This chapter assumes you know basic HTML structure and attributes, and that you can select elements and add event listeners in JavaScript. It covers the src attribute on audio and video, how the browser loads the file, and the errors you will meet most often.
The src attribute
The src attribute holds the URL of the media file. It works like src on an img, with one difference. The browser does not download the whole file up front. It fetches what it needs to start playback, and fetches more as playback continues.
<audio controls src="podcast-episode.mp3">
<a href="podcast-episode.mp3">Download the episode</a>
</audio>The URL can be relative or absolute. A relative URL resolves against the page's address, so src="media/clip.mp4" on https://example.com/lessons/ loads https://example.com/lessons/media/clip.mp4.
What happens after you set src
Setting src starts the browser's resource selection. The element moves through a series of states that you can observe from JavaScript.
The loadedmetadata event fires once the browser knows the duration and, for video, the dimensions. The canplay event fires when enough data has arrived to begin playback. Read duration only after loadedmetadata. Before then it is NaN.
Changing src from JavaScript
You can change the source at runtime by assigning to the src property. Call load() only when you change source elements. Assigning src already restarts loading.
const audio = document.querySelector('audio');
audio.addEventListener('error', () => {
console.error('Media failed to load, code:', audio.error.code);
});
audio.src = 'podcast-episode-2.mp3';Diagnosing load errors
When loading fails, the element fires an error event and sets element.error to a MediaError. Its code property tells you the category.
| Code | Name | Typical cause |
|---|---|---|
| 1 | `MEDIA_ERR_ABORTED` | Loading was stopped, for example by a script |
| 2 | `MEDIA_ERR_NETWORK` | The connection dropped during download |
| 3 | `MEDIA_ERR_DECODE` | The file is corrupt or uses features the decoder cannot handle |
| 4 | `MEDIA_ERR_SRC_NOT_SUPPORTED` | Wrong URL, wrong MIME type, or an unsupported format |
Code 4 is the most common surprise. It covers a 404 response as well as a file the browser cannot decode, so check the Network panel in your developer tools before assuming the format is the problem.
Common mistakes
- Using a file path that works on your machine but not on the server, such as a path with a different letter case.
- Setting
srcon the element and also addingsourcechildren. Thesrcattribute wins and thesourcechildren are ignored. - Reading
durationorvideoWidthbeforeloadedmetadatafires. - Opening the page from
file://and expecting features that need a server, such as seeking, to work.
A single src is fine when you control the file and know every target browser can play it. The next step is offering several formats, which is where source elements come in.
Exercise
A page sets src="lesson.mp4" on a video and the error event fires with code 4. Name two different causes you would check first.
Show answer
The URL may be wrong, so the server returns a 404 (check the Network panel). Or the file may be in a format or codec the browser cannot play, or be served with a wrong MIME type.