javascript - Web Audio API - 加载文件并播放

标签 javascript audio web

SO 上有一些类似的问题,但似乎语法已全部弃用。

我正在尝试从 DropBox(公共(public)链接)加载声音并在应用程序启动时播放它。

这是我的代码:

var context = new webkitAudioContext();
function start() {
// Note: this will load asynchronously
var request = new XMLHttpRequest();
request.open("GET", "https://dl.dropboxusercontent.com/u/86304379/Pt.%205%20Flood%20the%20Soul.wav", true);
request.responseType = "arraybuffer"; // Read as binary data
// Asynchronous callback
request.onload = function() {
    var data = request.response;

    audioRouting(data);
};
request.send();
}
function audioRouting(data) {
    source = context.createBufferSource(); // Create sound source
    buffer = context.createBuffer(data, true/* make mono */); // Create source buffer from raw binary
    source.buffer = buffer; // Add buffered data to object
    source.connect(context.destination); // Connect sound source to output
    playSound(source); // Pass the object to the play function
}
    // Tell the Source when to play
function playSound() {
    source.noteOn(context.currentTime); // play the source immediately
}

唉 - 我在我的一个函数中调用 playSound() 并得到了

“无法调用未定义的方法‘noteOn’”

这是因为 noteOn 已被弃用吗?或者是有其他问题。我只想将一些音频加载到音频节点中并播放它 - 我没有在网上找到任何足够的代码来显示如何执行此操作。

最佳答案

你是对的,noteOn功能确实消失了,现在是start:

user:        crogers
date:        Tue Sep 25 12:56:14 2012 -0700
summary:     noteOn/noteOff changed to start/stop -- added deprecation notes

该 API 与您从文档中获得的一样好 - AudioBufferSourceNode部分是您想要的部分。

Here's我如何做到这一点的一个例子(请原谅 CoffeeScript)。

关于javascript - Web Audio API - 加载文件并播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275315/

相关文章:

java - 通过绑定(bind)的前台服务保持音频播放

web - 可用于 Web 的实时通信协议(protocol)有哪些?

html - 图像映射背景图像?

javascript - oracle apex 5.0中如何防止提交页面后无法点击?

javascript - 按降序对多维数组进行排序

javascript - 返回 PartialView 时在脚本内使用 TempData

android - 尽管设置了手机静音模式,但在 Android 通知中播放声音

html - 音频标签在 Safari 中不起作用

multithreading - 在Java中的RESTful Web服务上使用线程

javascript - 如何抓取可拖动数据并附加到另一个 div