Av Met Canplaytype
## HTML Audio/Video DOM canPlayType() Method
The `canPlayType()` method is a built-in HTML Media Element method used to detect whether the user's browser is capable of playing a specified audio or video MIME type (and optional codecs).
---
## Introduction
Because different browsers support different media formats and codecs, you cannot always guarantee that a single video or audio file will play on every device. The `canPlayType()` method allows developers to programmatically query the browser's media capabilities before attempting to load and play a media source.
### Return Values
Instead of returning a simple boolean (`true`/`false`), this method returns one of three string values representing the level of confidence:
* **`"probably"`**: The browser is highly likely to support this media type and codec.
* **`"maybe"`**: The browser might be able to play this media type, but it cannot be certain without attempting to load the file (often returned when a MIME type is provided without specific codecs).
* **`""` (Empty String)**: The browser definitely does not support this media type.
---
## Syntax
```javascript
mediaElement.canPlayType(type)
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `type` | *String* | **Required.** The MIME type (and optional codecs) of the media format you want to test. |
### Common MIME Types and Codecs
When querying `canPlayType()`, you can pass a simple MIME type or a highly specific MIME type containing codec parameters.
#### Common MIME Types (Without Codecs)
* `video/ogg`
* `video/mp4`
* `video/webm`
* `audio/mpeg`
* `audio/ogg`
* `audio/mp4`
#### Common MIME Types with Codecs
* `video/ogg; codecs="theora, vorbis"`
* `video/mp4; codecs="avc1.4D401E, mp4a.40.2"`
* `video/webm; codecs="vp8.0, vorbis"`
* `audio/ogg; codecs="vorbis"`
* `audio/mp4; codecs="mp4a.40.5"`
> **Note:** The browser can only return `"probably"` if you include specific codecs in the parameter string. If you only provide the container type (e.g., `"video/mp4"`), the browser will return `"maybe"` at best.
---
## Code Examples
### Basic Detection Example
The following example dynamically creates a video element and checks if the browser can play an MP4 video with H.264 video and AAC audio codecs.
```javascript
// Create an off-screen video element
const video = document.createElement('video');
// Define the media type with codecs
const mediaType = 'video/mp4; codecs="avc1.4D401E, mp4a.40.2"';
// Check support
const supportLevel = video.canPlayType(mediaType);
if (supportLevel === "probably") {
console.log("Great! The browser highly likely supports this format.");
} else if (supportLevel === "maybe") {
console.log("The browser might support this format, but we are not 100% sure.");
} else {
console.log("The browser does not support this format.");
}
```
### Practical Fallback Selection
You can use `canPlayType()` to choose the best source file to load dynamically:
```javascript
function getSupportedSource(sources) {
const video = document.createElement('video');
for (let source of sources) {
const playability = video.canPlayType(source.type);
if (playability === 'probably' || playability === 'maybe') {
return source.url; // Return the first compatible source
}
}
return null; // No compatible sources found
}
const videoSources = [
{ url: 'movie.webm', type: 'video/webm; codecs="vp9, vorbis"' },
{ url: 'movie.mp4', type: 'video/mp4; codecs="avc1.4D401E, mp4a.40.2"' }
];
const optimalSourceUrl = getSupportedSource(videoSources);
```
---
## Browser Support
| Feature | Chrome | Edge/IE | Firefox | Opera | Safari |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **`canPlayType()`** | Yes | Yes (IE 9+) | Yes | Yes | Yes |
* **Note:** Internet Explorer 8 and earlier versions do not support the HTML5 `
YouTip