xorg1990
New member
Hi, es gibt eine neue API, mit der man Daten vom Server holen kann die fetch API.
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
man dann damit auch audio daten zerteilen:
Mit nen normalem xhr klappt das nicht, da müsste man das komplette file in den Browser laden und dann dekodieren.
Dann auch im kommen die OffscreenCanvas API,
https://developer.mozilla.org/de/docs/Web/API/OffscreenCanvas
Leider:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
man dann damit auch audio daten zerteilen:
Code:
//
// loads remote file using fetch() streams and "pipe" it to webaudio API
// remote file must have CORS enabled if on another domain
//
// mostly from http://stackoverflow.com/questions/20475982/choppy-inaudible-playback-with-chunked-audio-through-web-audio-api
//
function play(url) {
var context = new (window.AudioContext || window.webkitAudioContext)();
var audioStack = [];
var nextTime = 0;
fetch(url).then(function(response) {
var reader = response.body.getReader();
function read() {
return reader.read().then(({ value, done })=> {
context.decodeAudioData(value.buffer, function(buffer) {
audioStack.push(buffer);
if (audioStack.length) {
scheduleBuffers();
}
}, function(err) {
console.log("err(decodeAudioData): "+err);
});
if (done) {
console.log('done');
return;
}
read()
});
}
read();
})
function scheduleBuffers() {
while ( audioStack.length) {
var buffer = audioStack.shift();
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
if (nextTime == 0)
nextTime = context.currentTime + 0.01; /// add 50ms latency to work well across systems - tune this if you like
source.start(nextTime);
nextTime += source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
};
}
}
var url = 'http://www.metadecks.org/software/sweep/audio/demos/beats1.wav?adazdaz'
play(url);
Mit nen normalem xhr klappt das nicht, da müsste man das komplette file in den Browser laden und dann dekodieren.
Dann auch im kommen die OffscreenCanvas API,
https://developer.mozilla.org/de/docs/Web/API/OffscreenCanvas
Leider:
Aber in chrome scheint die 2d api zu funktionieren.This API is currently implemented for WebGL1 and WebGL2 contexts only. See bug 801176 for Canvas 2D API support from workers.