Use javascript to detect if an MP4 video has a sound track

后端 未结 1 1111
抹茶落季
抹茶落季 2021-02-15 16:31

I am creating a custom controller for MP4 video on a web page. The controller includes a volume slider. Some of the videos that are to be played have no sound track. It would be

相关标签:
1条回答
  • 2021-02-15 17:03

    There might be a better way of doing this, although it's fairly simple just using regular javascript for webkit or mozilla enabled browsers. webkit utilizes this.audioTracks and mozilla uses this.mozHasAudio respectively:

    document.getElementById("video").addEventListener("loadeddata", function() {
      if ('WebkitAppearance' in document.documentElement.style)
        var hasAudioTrack = this.audioTracks.length;
      else if (this.mozHasAudio)
        var hasAudioTrack = 1;
      if (hasAudioTrack > 0)
        alert("audio track detected");
      else
        alert("audio track not detected");
    });
    <video id="video" width="320" height="240" controls>
      <source src="http://media.w3.org/2010/05/video/movie_300.mp4" type="video/mp4">
    </video>

    There's also a function this.webkitAudioDecodedByteCount, however, I've never had any luck making it work.

    0 讨论(0)
提交回复
热议问题