简体   繁体   English

如何重用已经下载的浏览器的音频,这样它就不会在使用相同音频文件的其他页面中再次下载它?

[英]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?

I'm making an audio heavy page. 我正在制作音频沉重的页面。 I'm using the JavaScript audio element. 我正在使用JavaScript音频元素。 The code looks like this: 代码如下:

JavaScript: 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: 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. 某些页面最多使用30个不同的音频文件,因此会消耗很多带宽。 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. 谢谢你的时间。

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. 在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 . 除了适当的缓存之外(或除了适当的缓存之外),您还可以使用另一件事,就是不真正地重新加载页面,而是像这个简单的演示中那样使用AJAX重新加载页面的一部分。 A lot of JavaScript frameworks, like jQuery, YUI, Prototype, Dojo, etc. will help you with that. 许多JavaScript框架(例如jQuery,YUI,Prototype,Dojo等)将为您提供帮助。

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

相关问题 如何在nodejs中使用npm请求下载音频文件 - How can I download audio file using npm request in nodejs 如何更改此代码,以便我可以在同一页面上再次使用它 - How to change this code so I can use it again on the same page 我们如何使音频在所有浏览器中都能工作,如果浏览器不支持音频,我们可以使用什么原语? - How can we make audio works in all browsers, what primitive can we use if a browser doesn't support it? 用户如何上传音频以在浏览器中使用? - How can a user upload audio for use in the browser? 如何使用 Audio API 对相同的声音文件进行分层? - How can I layer the same sound file with Audio API? 如何在不使用相同音频 ID 的情况下播放从数据库中获取的多个音频文件 - How can i play multiple audio file gotten from a database without using same audio id 我无法重新启动音频文件 - I can't restart my audio file index.html 可以播放音频文件吗? 如果是这样,我如何实现它在后台自动播放? - Can index.html play an audio file? If so, How can I implement it to auto play in the back ground? 如何将 arrayBuffer 作为音频文件播放? - How can I play an arrayBuffer as an audio file? 我无法使用方法 var audio=new Audio(“Audio track”); 播放音频音频播放() - I can't play audio using the method var audio=new Audio(“Audio track”); audio.play()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM