Audio canPlayType() Method - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

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

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

  • "probably" - the browser most likely supports this audio type
  • "maybe" - the browser might support this audio type
  • "" - the browser does not support this audio type

Parameter Values

Value Description
type audio type and optional codecs to test support for.

Common values for type:

  • audio/mpeg
  • audio/ogg
  • audio/mp4

Common values, including codecs:

  • audio/ogg; codecs="vorbis"
  • audio/mp4; codecs="mp4a.40.5"
  • This method can return "probably" if codecs are included.

Return Value:

A String, representing the level of support.

Possible return values:

  • "probably" - most likely support
  • "maybe" - might support
  • "" - no support

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="supportType(event,'audio/ogg','vorbis')" type="button">Test</button>
</span></p>

<script>
function supportType(e,vidType,codType) {
    var x = document.createElement("AUDIO");
    isSupp = x.canPlayType(vidType+';codecs="'+codType+'"');
    if (isSupp == "") {
        isSupp = "No";
    }/*from www . j  a v  a2  s.c  om*/
    e.target.parentNode.innerHTML = "Answer: " + isSupp;
}
</script>

</body>
</html>

Related Tutorials