How can I reuse the audio a browser already downloaded so it doesn't download it again in other pages that use the same audio file?

非 Y 不嫁゛ 提交于 2019-12-23 03:22:39

问题


I'm making an audio heavy page. I'm using the JavaScript audio element. The code looks like this:

JavaScript:

var x = new Audio("x.mp3");
var y = new Audio("y.mp3");
var z = new Audio("z.mp3");

function playMe(n){
    n.play();
}

HTML:

<button class="button" onclick="playMe(x)")>Play</button>
<button class="button" onclick="playMe(y)")>Play</button>
<button class="button" onclick="playMe(z)")>Play</button>

Some pages use up to 30 different audio files so this will consume a lot of bandwith. This can potentially kill the project, as it's a one-man effort with almost no budget.

The audios are being downloaded each time the audio object is declared. Is there a way to reuse the audio that was already downloaded in other pages to be used on other pages without downloading it again?

Thanks for your time.


回答1:


You can use the Cache manifest to make sure that every resource that you want is downloaded only once and cached properly, even for offline browsing. See: "Offline": What does it mean and why should I care? on HTML5Rocks.

Another thing that you can use instead of (or in addition to) proper caching is having your page don't really reload but having only parts of it reloaded using AJAX like in this simple demo. A lot of JavaScript frameworks, like jQuery, YUI, Prototype, Dojo, etc. will help you with that.



来源:https://stackoverflow.com/questions/12551302/how-can-i-reuse-the-audio-a-browser-already-downloaded-so-it-doesnt-download-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!