音频api
The Web Audio API allows developers to load and decode audio on demand using JavaScript. The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch. The following is a very basic introduction to the WebAudio API, often used for JavaScript games or apps allowing modification of audio on the front end.
Web Audio API允许开发人员使用JavaScript按需加载和解码音频。 我对Firefox OS电视的出色游戏进行的评估越多,我就越了解这些通常不会碰到的API。 以下是对WebAudio API的非常基本的介绍,该API通常用于JavaScript游戏或允许修改前端音频的应用程序。
Let's start with a reduced Web Audio API code sample:
让我们从简化的Web Audio API代码示例开始:
// Create an AudioContext instance for this sound var audioContext = new (window.AudioContext || window.webkitAudioContext)(); // Create a buffer for the incoming sound content var source = audioContext.createBufferSource(); // Create the XHR which will grab the audio contents var request = new XMLHttpRequest(); // Set the audio file src here request.open('GET', 'sound-effect.mp3', true); // Setting the responseType to arraybuffer sets up the audio decoding request.responseType = 'arraybuffer'; request.onload = function() { // Decode the audio once the require is complete audioContext.decodeAudioData(request.response, function(buffer) { source.buffer = buffer; // Connect the audio to source (multiple audio buffers can be connected!) source.connect(audioContext.destination); // Simple setting for the buffer source.loop = true; // Play the sound! source.start(0); }, function(e) { console.log('Audio error! ', e); }); } // Send the request which kicks off request.send();I've tried to comment within the code to describe the process as best as possible. Remember that the above is a very reduced example; there's much more you can do with Web Audio.
我试图在代码中添加注释,以尽可能最好地描述该过程。 请记住,以上只是一个简化的示例; 使用Web Audio可以做更多的事情。
View Demo 观看演示I won't pretend to be an expert of all the nuances with Web Audio -- I was simply super excited to see all of this working. Web Audio does way more than simply allow you to play the audio; you can modify the audio with filters, ramping, and a bunch more. You can learn more about WebAudio from these awesome sources:
我不会假装成为Web Audio的所有细微差别的专家-看到所有这些工作真是让我感到非常兴奋。 Web Audio的功能远不止让您播放音频。 您可以使用过滤器,渐变等来修改音频 。 您可以从以下很棒的来源中了解有关WebAudio的更多信息:
Web Audio API
网络音频API
Using the Web Audio API
使用网络音频API
Getting Started with Web Audio API
Web Audio API入门
Have you done anything awesome with Web Audio? Please share with the noobs like me!
您使用Web Audio做过什么很棒的事情吗? 请与我这样的菜鸟分享!
翻译自: https://davidwalsh.name/web-audio-api
音频api
相关资源:web-audio-api, 由W3C音频小组开发的网络音频 API.zip