【问题标题】:how to insert audio in a webpage and hide the control in html如何在网页中插入音频并在html中隐藏控件
【发布时间】:2019-03-10 17:51:36
【问题描述】:

如何在不显示任何控件的情况下插入本地音频(如果我们只能显示音量按钮则更好),它会不断循环...? 我尝试了嵌入标签、iframe 标签、音频标签,但没有人是完美的,有些作品在边缘但不是在 chrome 上......

【问题讨论】:

标签: html audio controls


【解决方案1】:

尝试使用 CSS 将“显示”设置为“无”来隐藏它。

audio { 
   display:none;
}

【讨论】:

    【解决方案2】:

    您可以使用 Web Audio API 在 html 页面中播放声音,而无需可见控件。
    loop选项可以激活循环播放。

    const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
    let source = audioCtx.createBufferSource();
    
    fetch('sample.wav')
      .then(response => response.arrayBuffer())
      .then(buffer => {
        audioCtx.decodeAudioData(buffer, data => {
          source.buffer = data;
          source.connect(audioCtx.destination);
          source.loop = true;
          source.start(0);
        })
      })
    

    如果需要,您可以借助 gainNode 和范围滑块添加音量控制。

    请注意,某些浏览器实现会在用户明确允许之前阻止播放音频。

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2020-08-30
      相关资源
      最近更新 更多