How to check if the browser can play mp4 via html5 video tag?

Viewed 46850

How to check if the browser can play mp4 via html5 video tag?

5 Answers

I made a function to validate if your browser is able to play audio or video element

    const checkMedia = mediaType => {
    let canPlay = false
    const mediaFormat = {
      audio: 'audio/mp3',
      video: 'video/mp4'
    }
      let media = document.createElement(mediaType)
    if (
      media.canPlayType &&
      media.canPlayType(mediaFormat[mediaType]).replace(/no/, '')
    ) {
      canPlay = true
    }
      return canPlay
    }
Related