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

后端 未结 1 1115
抹茶落季
抹茶落季 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");
    });

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

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