Saptarshi Sahoo
Saptarshi Sahoo

Reputation: 97

Audio loading with XMLHttpRequest in JavaScript

I am learing Web Audio API. Im having a problem in loading my sound file thourgh XMLHttpRequest in JavaScript. Kindly please help.Here is my code

var source;

function start() {
  console.log("WELCOME!!");
  try{
    var actx = new AudioContext();
  }catch(e){
    console.log('WebAudio api is not supported!!');
  }

  source = actx.createBufferSource()

  var req  = new XMLHttpRequest();
  req.open('GET','src3.ogg',true);
  req.responseType='ArrayBuffer';

  req.onload = function(){
    var audioData = req.response;
    actx.decodeAudioData(audioData,function(buffer){
      source.buffer = buffer;
      source.connect(actx.destination);
      source.loop();
    },
    function(e){
      console.log('Error in decoding audio'+e.err);
    }

  );
  }
  req.send();
}

function play() {
  source.start(0);
}
function stop(){
  source.stop(0);
}

Browser is showing this error this:

XML Parsing Error: not well-formed
Location: file:///home/uzumaki/Web_Audio_Projects/src3.ogg
Line Number 1, Column 5:

And start() method is called on the onload event of body tag

Upvotes: 0

Views: 865

Answers (1)

guest271314
guest271314

Reputation: 1

Substitute

req.responseType = "arraybuffer";

for

req.responseType = "ArrayBuffer";

.responseType is not converted to lowercase by XMLHttpResponse instance

Upvotes: 1

Related Questions