7

I have a problem I've been working on for a while now with no real headway. I am currently trying to load my Shoutcast stream into my Web Audio API Context. I figured it would violate the CORS and I was right. I tried both through an XHR Request and then again through loading an audio element into the script. It worked with the audio element, but died when trying to load it into the script. It seems my only option is to try and somehow add the CORS headers to the stream that my Shoutcast is serving.

I have no idea how to do this and have had no luck finding resources online. If anyone can give me some advice on this it would be much appreciated!

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var soundSource, concertHallBuffer;

ajaxRequest = new XMLHttpRequest();

ajaxRequest.open('GET', 'http://127.0.0.1:8000/;stream.mp3', true);

ajaxRequest.responseType = 'arraybuffer';


ajaxRequest.onload = function() {
  var audioData = ajaxRequest.response;
  console.log(audioData);

   audioCtx.decodeAudioData(audioData, function(buffer) {
    concertHallBuffer = buffer;
    soundSource = audioCtx.createBufferSource();
    soundSource.buffer = concertHallBuffer;

    soundSource.connect(audioCtx.destination);
    soundSource.loop = true;
    soundSource.start();
  }, function(e){"Error with decoding audio data" + e.err});
}

ajaxRequest.send();
Brad
  • 159,648
  • 54
  • 349
  • 530
Monty Monro
  • 603
  • 2
  • 10
  • 22
  • Possible duplicate: http://stackoverflow.com/questions/30603872/how-can-i-avoid-cors-restriction-for-web-audio-api – Anson Kao Jun 08 '15 at 01:05

1 Answers1

4

Just to answer my own question. I was able to get this working fine with the shoutcast stream by setting up a reverse proxy in apache that pointed to my shoutcast stream and port.

Monty Monro
  • 603
  • 2
  • 10
  • 22