How can I force an HTML5 audio element to buffer an entire song?

前端 未结 3 1758
轮回少年
轮回少年 2021-02-01 23:10

I\'m developing a local server that will stream a user\'s audio files so they can access them via web browsers using the HTML5 audio object. Since these files are on the user\'s

相关标签:
3条回答
  • 2021-02-01 23:25

    You could set the preload="auto" attribute. It's not guaranteed to do anything, but it's supposed to tell the user agent to buffer as much as it wants without concern for the remote end. http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#attr-media-preload

    0 讨论(0)
  • 2021-02-01 23:31

    You can use the load() method. This basically forces preload="auto". But it probably won't buffer the whole thing.

    I had the issue that audio wasn't preloaded on mobile devices (even with preload=auto), but this method worked.

    http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dfnReturnLink-2

    Another option is to load the data via an XMLHttpRequest as a binary blob and set the <audio> element's src attribute to the blob URI.

    (I personally don't really like the play/pause hack.)

    0 讨论(0)
  • 2021-02-01 23:42

    The solution I found was this:

    function load() {
        a.play();
        setTimeout("a.pause()", 10);
    }
    

    Play the file and pause it 10ms later, then the browser will buffer the entire song.

    0 讨论(0)
提交回复
热议问题