Video canPlayType() Method - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Video

Description

The canPlayType() method checks if the browser can play the specified video type.

The canPlayType() method can return one of the following values:

Value Meaning
"probably" the browser most likely supports this video type
"maybe" the browser might support this video type
"" the browser does not support this video type

Parameter Values

Value Description
type Specifies the video type (and optional codecs) to test support for.

Common values:

  • video/ogg
  • video/mp4
  • video/webm

Common values, including codecs:

  • video/ogg; codecs="theora, vorbis"
  • video/mp4; codecs="avc1.4D401E, mp4a.40.2"
  • video/webm; codecs="vp8.0, vorbis"

Return Value:

A String, representing the level of support.

The following code shows how to Check if your browser can play different types of video:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Can my browser play MP4 videos? <span>
<button onclick="supportType(event,'video/mp4','avc1.42E01E, mp4a.40.2')" type="button">Test</button>
</span></p>

<p>Can my browser play OGG videos? <span>
<button onclick="supportType(event,'video/ogg','theora, vorbis')" type="button">Test</button>
</span></p>

<script>
function supportType(e,vidType,codType) {
    var x = document.createElement("VIDEO");
    isSupp = x.canPlayType(vidType+';codecs="'+codType+'"');
    if (isSupp == "") {
        isSupp = "No";
    }//from w ww. j a  v  a 2  s  .  co  m
    e.target.parentNode.innerHTML = "Answer: " + isSupp;
}
</script>

</body>
</html>

Related Tutorials