HTML5视频,如何在没有音轨时检测? [英] HTML5 video, how to detect when there is no audio track?

查看:202
本文介绍了HTML5视频,如何在没有音轨时检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Chrome应用程序,我想为视频播放提供自定义控件,但我在使用静音按钮方面遇到了一些困难。大多数将在应用程序中播放的视频都是静音的,因此我希望能够在没有音频轨道的情况下禁用该按钮,就像Chrome使用默认控件一样。

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.

尝试使用音量值,但即使没有音轨也会返回1。检查视频是否静音也不起作用。

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.

这是代码段

有任何建议吗?

推荐答案

在某些时候,浏览器可能会开始实施 audioTracks属性。目前,您可以为webkit使用 webkitAudioDecodedByteCount ,为firefox使用 mozHasAudio

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");
});

这篇关于HTML5视频,如何在没有音轨时检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆