有没有办法在使用Howler.js时指示音频下载进度/缓冲区?

时间:2016-06-11 23:59:38

标签: javascript html5-audio web-audio howler.js

我正在使用Howler.js并尝试完成以下任务:

download with buffer

然而,咆哮似乎没有audio "progress" events的支持。有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

至少在实现此功能之前(如果有的话),您需要绑定到Howler的非公开API上。

const audio = new Howl({
  src: 'https://howlerjs.com/assets/howler.js/examples/player/audio/rave_digger.webm',
  html5: true,
  preload: false,
});

const handleLoad = () => {
  const node = audio._sounds[0]._node;
  // const node:HTMLAudioElement = (audio as any)._sounds[0]._node; // For Typescript
  node.addEventListener('progress', () => {
    const duration = audio.duration();

    // https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery/buffering_seeking_time_ranges#Creating_our_own_Buffering_Feedback
    if (duration > 0) {
      for (let i = 0; i < node.buffered.length; i++) {
        if (node.buffered.start(node.buffered.length - 1 - i) < node.currentTime) {
          const bufferProgress = (node.buffered.end(node.buffered.length - 1 - i) / duration) * 100;
          // do what you will with it. I.E - store.set({ bufferProgress });
          break;
        }
      }
    }
  }
};

audio.on('load', handleLoad);

可以在heregoldfire/howler.js中找到原始答案