[英]Optimize audio for iOS Web App
我目前正在使用Cordova框架使用Javascript开发和测试iOS游戏。 我试图在触摸某些节点时添加声音效果。 由于可以以任何速率重复触摸节点。 我正在使用...
var snd = new Audio("audio/note_"+currentChain.length+".mp3");
snd.play();
哪个可以满足我的需要,但是启用这些效果后,我发现游戏会滞后。 我正在处理的mp3文件已缩小到大约16kb,甚至仍然很滞后。
在我的情况下优化声音的最佳方法是什么? 因为应用程序不是本地的,我是否对质量有所限制?
谢谢!
最好是预加载它们,并在需要时准备它们。 我刚刚写了一个快速的自包含的闭包,我认为它将向您展示大部分您想知道的操作方法。
var playSound = (function () {
var sounds = 15, // idk how many mp3 files you have
loaded = new Array(sounds),
current = -1,
chain = [],
audio,
i;
function incrementLoaded(index) {
loaded[index] = true;
}
// preloads the sound data
for(i = 0; i < sounds; i++) {
audio = new Audio();
audio.addEventListener('canplay', incrementLoaded.bind(audio, i));
audio.src = 'audio/note_' + i + '.mp3';
chain.push(audio);
}
// this will play audio only when ready and in sequential order automatically
// or "which" index, if supplied
return function (which) {
if(typeof which === 'number') {
current = which;
} else {
current = (current + 1) % sounds;
}
// only play if loaded
if(loaded[current]) {
chain[current].pause();
chain[current].currentTime = 0;
chain[current].play();
}
};
}());
// would play sounds in order every second
setInterval(function () {
playSound();
}, 1000);
如果使用多个文件,建议您使用声音精灵的概念将其更改为单个文件。 此链接具有更多详细信息: http : //www.ibm.com/developerworks/library/wa-ioshtml5/
以我自己的经验,如果您没有使声音准确播放到想要的位置,请尝试提高文件比特率,请参考: http : //pupunzi.open-lab.com/2013/03/13/making-html5-音频实际工作,对移动/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.