Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shoutcast + Web Audio API CORS issue

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();
like image 760
Monty Monro Avatar asked May 26 '15 09:05

Monty Monro


1 Answers

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.

like image 158
Monty Monro Avatar answered Oct 02 '22 07:10

Monty Monro