Sub topic 15
Detecting format support with canPlayType
This chapter assumes you know basic HTML attributes, how to select elements and attach event listeners in JavaScript, and the source element from earlier chapters. It shows how to ask the browser whether it can play a given format before you choose a file.
Why detect support
The source element already lets the browser pick the first file it can play. Detection is useful when you need to decide in code. For example, you might choose which file to request, show a "your browser can't play this" message, or pick a quality level. The tool for this is the canPlayType() method on media elements.
What canPlayType returns
canPlayType(type) takes a MIME type string and returns one of three strings:
| Return value | Meaning |
|---|---|
| `""` (empty string) | The browser cannot play this type. |
| `"maybe"` | The container looks playable, but the codecs are unknown or not given. |
| `"probably"` | The browser believes it can play this exact container and codec combination. |
The method never returns true or false. An empty string is falsy, but "maybe" and "probably" are both truthy, so if (video.canPlayType(...)) works as a basic yes/no test.
A first probe
You don't need a video on the page. Create a throwaway element and ask it.
const probe = document.createElement('video');
console.log(probe.canPlayType('video/webm; codecs="vp9, opus"'));
console.log(probe.canPlayType('video/mp4'));The first call names codecs, so a supporting browser can answer "probably". The second names only the container. MP4 can hold many codecs, so the best a browser can honestly say is "maybe". Exact answers differ between browsers and operating systems.
Include codecs in the type
The codecs parameter makes the answer more precise. A WebM file with VP9 video may play where a WebM file with a different codec will not. Always pass the codec string when you know it.
function bestSource(candidates) {
const probe = document.createElement('video');
let fallback = null;
for (const { src, type } of candidates) {
const answer = probe.canPlayType(type);
if (answer === 'probably') return src;
if (answer === 'maybe' && fallback === null) fallback = src;
}
return fallback;
}
const chosen = bestSource([
{ src: 'clip.webm', type: 'video/webm; codecs="vp9, opus"' },
{ src: 'clip.mp4', type: 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"' },
]);
if (chosen) {
document.querySelector('video').src = chosen;
} else {
console.warn('No supported video format found.');
}The function returns the first "probably" match. If none exists, it falls back to the first "maybe". If both are missing, it returns null, and the page can show fallback content.
Common mistakes
- Comparing the result to
true. The method returns strings, so=== trueis always false. - Treating
"maybe"as a failure. Many working formats report"maybe"when you leave out the codecs. - Using
canPlayTypewheresourceis enough. If you only need the first playable file, list severalsourceelements withtypeattributes and let the browser choose. - Trusting it as a guarantee.
"probably"is a hint. A damaged file or a wrong server MIME type can still fail, so listen for theerrorevent as well.
Exercise
What does video.canPlayType('video/mp4') most likely return in a browser that supports MP4, and why is it not "probably"?
Show answer
It most likely returns "maybe". The string names only the container, and MP4 can hold codecs the browser may not support. Adding a codecs parameter, such as video/mp4; codecs="avc1.42E01E, mp4a.40.2", lets the browser give a more confident answer.