1

我一直在使用Web Audio Api,这就是我迄今为止加载/缓冲声音文件的工作。

function loadSound(url) {
            // Load buffer asynchronously
            var request = new XMLHttpRequest();
            request.open("GET", url, true);
            request.responseType = "arraybuffer";

            request.onload = function() {
                // Asynchronously decode the audio file data
                context.decodeAudioData(
                    request.response,
                    function(buffer) {
                        if (!buffer) {
                            console.log('error decoding file data: ' + url);
                            return;
                        }
                            window.kick = buffer;
                    }
                );
            }

            request.onerror = function() {
                console.log('error loading file data: ' + url);
            }

            request.send();
        }

然而,我的问题是我试图window.kick在 loadSound 函数中变成一个参数。比如loadSound(url, sourceName)哪里sourceName会接管window.kick。最终目标是按名称播放声音,例如note(sourceName);(目前正在使用note(kick);上面的代码)

这样做的原因是我必须在整个应用程序的不同时间加载声音,而且它们不能一次全部加载。然后需要在之后按名称调用它们,与按索引调用并在开头调用的情况不同。

4

1 回答 1

1
function loadSound(url, sourceName) {
            // Load buffer asynchronously
            var request = new XMLHttpRequest();
            request.open("GET", url, true);
            request.responseType = "arraybuffer";

            request.onload = function() {
                // Asynchronously decode the audio file data
                context.decodeAudioData(
                    request.response,
                    function(buffer) {
                        if (!buffer) {
                            console.log('error decoding file data: ' + url);
                            return;
                        }
                            window[sourceName] = buffer;
                    }
                );
            }

            request.onerror = function() {
                console.log('error loading file data: ' + url);
            }

            request.send();
        }
于 2013-08-15T00:49:12.790 回答