javascript - 网络音频 api 和嵌套函数
问题描述
我尝试创建一个音频缓冲区,其长度取决于音频文件的长度/持续时间。
我使用网络音频 API https://webaudio.github.io/web-audio-api/#offlineaudiocontext
function getDuration(filename) {
// Send XHR request for audio file to server.
const request = new XMLHttpRequest();
request.open('GET', gorilla.stimuliURL(filename));
request.responseType = 'arraybuffer';
request.onload = function() {
// Decode the audio.
audioContext.decodeAudioData(request.response, function(buffer) {
// Load and arm the source buffer.
var tStimNew = buffer.duration;
});
}
request.send();
}
我使用此功能获得音频文件的持续时间。我对其进行了测试,tStimNew 正是我所期望的。问题似乎是变量 tStimNew 只是函数的局部变量。但我想让变量 global 能够进一步使用它。
我必须在哪里/如何包含退货?这三个函数的嵌套对我来说太复杂了。
先感谢您!
解决方案
推荐阅读
- javascript - 用一个值替换第一次出现,用另一个值替换第二次
- javascript - 我想将所有请求重定向到主域
- react-native - 反应本机:没有调试模式的时刻不适用于特定时间值
- c++ - 从二叉搜索树中删除节点时遇到问题
- node.js - Web App 调用失败,并带有自定义 API 的“...错误代码 INTERNAL”
- ios - 如何为 Swift 3.0 专门安装 firebase pod
- matlab - 如何在 MATLAB 中获取或创建 .fig 文件
- javascript - Js过滤嵌套数组
- python-3.x - 提取 2 个字符 [ ] 之间的多个子字符串并在 Python 中替换
- multidimensional-array - ndarray:有没有一种有效的方法来交换 2 行/列/等。在不同的数组中?