简体   繁体   English

优化iOS Web App的音频

[英]Optimize audio for iOS Web App

I'm currently developing and testing a game for iOS using Javascript with the Cordova framework. 我目前正在使用Cordova框架使用Javascript开发和测试iOS游戏。 I'm attempting to add sound effects when certain nodes are touched. 我试图在触摸某些节点时添加声音效果。 Since nodes can be touched repeatedly at any rate. 由于可以以任何速率重复触摸节点。 I'm using... 我正在使用...

var snd = new Audio("audio/note_"+currentChain.length+".mp3");
snd.play();

Which works for what I need but when I enable these effects I find that the game lags. 哪个可以满足我的需要,但是启用这些效果后,我发现游戏会滞后。 I'm working with mp3 files that have been shrunken down to about 16kb in size and even still the lag is substantial. 我正在处理的mp3文件已缩小到大约16kb,甚至仍然很滞后。

What's is the best way to optimize sound in my situation? 在我的情况下优化声音的最佳方法是什么? Am I limited on quality because the application is not native? 因为应用程序不是本地的,我是否对质量有所限制?

Thanks! 谢谢!

It would be the best option to preload them and have them ready when needed. 最好是预加载它们,并在需要时准备它们。 I just wrote up a quick self-contained closure that I think will show you most of what you'd like to know how to do. 我刚刚写了一个快速的自包含的闭包,我认为它将向您展示大部分您想知道的操作方法。

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);

If you are using multiple files, I'd suggest you to change that to a single file, using the idea of sound sprites. 如果使用多个文件,建议您使用声音精灵的概念将其更改为单个文件。 This link has more tdetails about it: http://www.ibm.com/developerworks/library/wa-ioshtml5/ 此链接具有更多详细信息: http : //www.ibm.com/developerworks/library/wa-ioshtml5/

From my own experience, try increasing the file bitrate if you are not getting the sound to play exactly where you want it to, ref: http://pupunzi.open-lab.com/2013/03/13/making-html5-audio-actually-work-on-mobile/ 以我自己的经验,如果您没有使声音准确播放到想要的位置,请尝试提高文件比特率,请参考: http : //pupunzi.open-lab.com/2013/03/13/making-html5-音频实际工作,对移动/

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

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