简体   繁体   English

为使用 Hls.js 管理的 HLS 视频构建音量计

[英]Build a volume meter for an HLS video managed with Hls.js

I am using Hls.js to manage a video into my HTML page.我正在使用 Hls.js 将视频管理到我的 HTML 页面中。 I need to build a volume meter that inform the user about the audio level of the video.我需要构建一个音量计来通知用户视频的音频电平。 Since I need to keep the video.muted = true , I am wondering if the there is any way with Hls.js to extract the audio information from the stream and build a volume meter with those.由于我需要保留video.muted = true ,我想知道 Hls.js 是否有任何方法可以从 stream 中提取音频信息并用它们构建音量计。 The goal is give the users a feedback without have the volume of the video on.目标是在没有打开视频音量的情况下为用户提供反馈。

You can do this easily with the Web Audio API.您可以使用 Web 音频 API 轻松做到这一点。

Specifically, you'll want a couple nodes:具体来说,您需要几个节点:

  • MediaElementAudioSourceNode MediaElementAudioSourceNode
    You will use this to route the audio from your media element (ie the video element HLS.js is playing in) to the audio graph.您将使用它来将音频从您的媒体元素(即正在播放的视频元素 HLS.js)路由到音频图。

  • AnalyserNode 分析器节点
    This node analyzes the audio in chunks, giving you frequency data (via FFT) and time domain data.该节点分块分析音频,为您提供频率数据(通过 FFT)和时域数据。 The time domain data is simplified from the main stream.时域数据从主stream简化而来。 You can run a min/max on it to get a value (generally -1.0 to +1.0).您可以对其运行最小值/最大值以获得一个值(通常为 -1.0 到 +1.0)。 You can use that value in your visualization.您可以在可视化中使用该值。

You also need to connect the AnalyserNode to the AudioContext's destinationNode to output the audio in the end, since it will be re-routed from that video element.您还需要将 AnalyserNode 连接到 AudioContext 的destinationNode到 output 最后的音频,因为它将从该视频元素重新路由。

Note that this solution isn't particular to HLS.请注意,此解决方案并非特定于 HLS。 The same method works on any audio/video element, provided that the source data isn't tainted by cross-origin restrictions.相同的方法适用于任何音频/视频元素,前提是源数据不受跨域限制的污染。 Given how HLS.js works, you won't have to worry about that, since the CORS problem is already solved or it wouldn't work at all.鉴于 HLS.js 的工作原理,您不必担心这一点,因为 CORS 问题已经解决或者根本无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM