调用音频元素在html5中播放的更快方法 [英] faster way for calling audio element to play in html5

查看:101
本文介绍了调用音频元素在html5中播放的更快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在HTML5手机游戏中使用音频元素。
我有一些声音效果问题,所以我在游戏开始时多次克隆音频元素:

I'm trying to use audio elemnt in HTML5 mobile game. I have some problems with sound effects,so I clone the audio elemnt many times in the start of the game like that :

for (var i=0; i < 10; i++)
        {
            var container = document.getElementById("audioDiv");
            var clone = document.getElementById('multiaudio0').cloneNode(true);
            clone.setAttribute('id',"multiaudio" + i);
            container.appendChild (clone);
        };

然后当我需要碰撞使用它时,我将其称为NEXT:

Then when I need to use it "in collision" ,I call it as NEXT :

document.getElementById('audioDiv').getElementsByTagName("audio")[score%10].play();

在调用元素并播放它时,画布上的动画非常缓慢且速度慢的问题!!!!
有时候它也不起作用!!!

The problem that it is very slow and slow down the animation on canvas while calling the element and play it !!!! Some times also it do NOT work !!!

对于HTML5中的手机游戏(没有闪光灯)的任何想法,或快速通话的最佳做法对于元素而不是 document.getElementById ???

Any idea about sound for mobile games in HTML5 (without flash),or a best practice for fast calling for the element rather than document.getElementById ???

推荐答案

我建议在调用之前预先定义一个包含指向各种音频元素的指针的数组(而不是重复 document.getElementById('audioDiv')。getElementsByTagName(audio)多次查询
还要确保预先加载了音频样本,以便缓存它们,并且在第一次需要时不等网络资源

I would recommend pre-defining an array that contains the pointers to the various audio elements before you call one (rather than repeat the document.getElementById('audioDiv').getElementsByTagName("audio") lookup multiple times Also make sure you have pre-loaded the audio samples so they are cached and you're not waiting on network resources the first time you need them

如果您正在开发基于HTML5的游戏,我建议您查看EaselJS框架,尤其是SoundJS游戏...... http://www.createjs.com/#!/SoundJS

If you are working on an HTML5 based game I'd suggest you have a look at the EaselJS framework, especially the SoundJS pieces... http://www.createjs.com/#!/SoundJS

这篇关于调用音频元素在html5中播放的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆