Ho giocato con l'API Web Audio e sto provando a caricare più parti di un brano e aggiungerli a un nuovo ArrayBuffer e quindi utilizzarlo ArrayBuffer per suonare tutte le parti come una canzone. Nell'esempio seguente sto usando gli stessi dati di song (che è un piccolo loop) invece di parti diverse di una song.Le API Web Audio aggiungono/concatenano AudioBuffer diversi e li suonano come un brano
Il problema è che suona ancora una volta invece di due volte e non so perché.
function init() {
/**
* Appends two ArrayBuffers into a new one.
*
* @param {ArrayBuffer} buffer1 The first buffer.
* @param {ArrayBuffer} buffer2 The second buffer.
*/
function appendBuffer(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp;
}
/**
* Loads a song
*
* @param {String} url The url of the song.
*/
function loadSongWebAudioAPI(url) {
var request = new XMLHttpRequest();
var context = new webkitAudioContext();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
/**
* Appends two ArrayBuffers into a new one.
*
* @param {ArrayBuffer} data The ArrayBuffer that was loaded.
*/
function play(data) {
// Concatenate the two buffers into one.
var a = appendBuffer(data, data);
var buffer = a.buffer;
var audioSource = context.createBufferSource();
audioSource.connect(context.destination);
//decode the loaded data
context.decodeAudioData(buffer, function(buf) {
console.log('The buffer', buf);
audioSource.buffer = buf;
audioSource.noteOn(0);
audioSource.playbackRate.value = 1;
});
};
// When the song is loaded asynchronously try to play it.
request.onload = function() {
play(request.response);
}
request.send();
}
loadSongWebAudioAPI('http://localhost:8282/loop.mp3');
}
window.addEventListener('load',init,false);
Grazie, il tuo commento mi ha aiutato! http://72lions.github.com/PlayingChunkedMP3-WebAudioAPI/ – 72lions
@cwilso È possibile connettere più AudioBufferSourceNodes in uno solo in momenti diversi della riproduzione? –
Bene, puoi semplicemente collegarli alla stessa destinazione. Penso che abbia l'effetto che stai cercando. – cwilso