Mon*_*nro 6 javascript audio ajax shoutcast internet-radio
我有一个问题,我已经工作了一段时间,没有真正的进展.我目前正在尝试将我的Shoutcast流加载到我的Web Audio API上下文中.我认为它会违反CORS而我是对的.我尝试了两个XHR请求,然后再通过将音频元素加载到脚本中.它与audio元素一起使用,但在尝试将其加载到脚本时死亡.似乎我唯一的选择是尝试以某种方式将CORS标头添加到我的Shoutcast服务的流中.
我不知道如何做到这一点,并没有运气在线寻找资源.如果有人能给我一些建议,我将不胜感激!
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();
Run Code Online (Sandbox Code Playgroud)