Javascript DOM HTML Video canPlayType() Method

Introduction

Check if your browser can play different types of video:

View 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   ww w  .jav a 2 s  . c  o  m
  e.target.parentNode.innerHTML = "Answer: " + isSupp;
}
</script>

</body>
</html>

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
"" (empty string) the browser does not support this video type
canPlayType(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"

This method can only return "probably" if codecs are included.

The canPlayType() method returns a String, representing the level of support.

Possible return values:

  • "probably" - most likely support
  • "maybe" - might support
  • "" - (empty string) no support



PreviousNext

Related