Posted By

tpryan on 02/16/12


Tagged


Versions (?)

Web Audio API code for loading audio at a particular speed.


 / Published in: JavaScript
 

Code required to play an audio file using Web Audio API

  1. function playAudio(file, speed){
  2. if (typeof AudioContext == "function") {
  3. var audioContext = new AudioContext();
  4. } else if (typeof webkitAudioContext == "function") {
  5. var audioContext = new webkitAudioContext();
  6. }
  7.  
  8. var source = audioContext.createBufferSource();
  9. source.connect(audioContext.destination);
  10.  
  11.  
  12. var xhr = new XMLHttpRequest();
  13. xhr.open("GET", file, true);
  14. xhr.responseType = "arraybuffer";
  15. xhr.onload = function() {
  16. var buffer = audioContext.createBuffer(xhr.response, false);
  17. source.buffer = buffer;
  18. source.playbackRate.value = speed;
  19. source.noteOn(0);
  20. };
  21. xhr.send();
  22. }

Report this snippet  

You need to login to post a comment.