Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 video, how to detect when there is no audio track?

I'm making a chrome app and I'd like to have custom controls for the video playback but I'm having some difficulties with the mute button. Most of the videos that will be played in the app are silent so I'd like to be able to disable the button when there is no audio track just like chrome does with the default controls.

Tried using the volume value but it returns "1" even though there's no audio track. Checking if the video is muted didn't work either.

Here's a snippet.

Any suggestions?

like image 801
Louis P. Taylor Avatar asked Jan 21 '14 22:01

Louis P. Taylor


People also ask

Why is my video not working in HTML5?

If your browser error "HTML5 video file not found", it means that your browser is not up to date or website pages does not have a suitable video codec. It would help if you communicated with the developer to solve the issue and install all the required codecs.

What is the correct HTML5 element for playing video files?

<video>: The Video Embed element. The <video> HTML element embeds a media player which supports video playback into the document.

What is HTML5 playback?

An HTML5 Video Player is a JavaScript library that builds a custom set of controls over top of the HTML5 video element to provide a consistent look between HTML5 browsers.

How does HTML5 video work?

HTML5 video works by allowing the person uploading the video to embed it directly into a web page. It works in a variety of internet browsers, including Internet Explorer 9+, Firefox, Opera, Chrome and Safari. Unfortunately, the technology is not compatible with Internet Explorer 8 and earlier versions.


2 Answers

Shorter function based on upuoth's answer and extended to support IE10+

function hasAudio (video) {
    return video.mozHasAudio ||
    Boolean(video.webkitAudioDecodedByteCount) ||
    Boolean(video.audioTracks && video.audioTracks.length);
}

Usage:

var video = document.querySelector('video');
if(hasAudio(video)) {
    console.log("video has audio");
} else{
    console.log("video doesn't have audio");
}
like image 73
fregante Avatar answered Sep 19 '22 13:09

fregante


At some point, browsers might start implementing the audioTracks property. For now, you can use webkitAudioDecodedByteCount for webkit, and mozHasAudio for firefox.

document.getElementById("video").addEventListener("loadeddata", function() {
  if (typeof this.webkitAudioDecodedByteCount !== "undefined") {
    // non-zero if video has audio track
    if (this.webkitAudioDecodedByteCount > 0)
      console.log("video has audio");
    else
      console.log("video doesn't have audio");
  }
  else if (typeof this.mozHasAudio !== "undefined") {
    // true if video has audio track
    if (this.mozHasAudio)
      console.log("video has audio");
    else
      console.log("video doesn't have audio");
  }
  else
    console.log("can't tell if video has audio");
});
like image 38
upuoth Avatar answered Sep 19 '22 13:09

upuoth