简体   繁体   English

HTML5 / Jquery - 预加载音频的正确方法是什么?

[英]HTML5/Jquery - What is the correct way to preload audio?

I've created a game which has a number of elements on a page, when the mouseover event on the element is triggered an audio file plays. 我创建了一个游戏,页面上有许多元素,当元素上的鼠标悬停事件被触发时,播放音频文件。

Its working, however, 然而,它的工作,

My question is, What is the correct way to preload audio? 我的问题是,预加载音频的正确方法是什么? So i can be sure that my audio plays as soon as the user interacts with the element. 所以我可以确保我的音频在用户与元素交互时立即播放。

I'm currently initialsing my audio object on mouseover 我目前正在mouseover上初始化我的音频对象

$('.circle').mouseover(function() {
    // retrieve ref from data- attribute
    var noteIndex = $(this).attr('data-note'); 

    // locate url from the array notes using noteIndex ref
    var snd = new Audio(notes[noteIndex]); 
    snd.play();
}

I'm aware of the Audio tags, but i'm unsure how that differs from my technique above. 我知道音频标签,但我不确定这与我上面的技术有什么不同。

EDIT : example of how i'm currently loading audio http://jsfiddle.net/kA5Bv/1/ (note the key doesn't play immediately, thats because the example audio files i've used has a gap of 1/2 second or so at the beginning) 编辑:我目前如何加载音频http://jsfiddle.net/kA5Bv/1/的例子(注意键不会立即播放,那是因为我使用的示例音频文件有1/2的差距开头的第二个左右)

Thanks in advance, Cam 提前谢谢,Cam

那么$(document).ready(function(){});它会在加载页面内容之前在DOM加载时初始化你的音频对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM