Tôi đã chơi với API âm thanh trên web và tôi đang cố gắng tải nhiều phần của một bài hát và thêm chúng vào ArrayBuffer mới và sau đó sử dụng ArrayBuffer để chơi tất cả các phần như một bài hát. Trong ví dụ sau tôi đang sử dụng cùng một dữ liệu bài hát (đó là một vòng lặp nhỏ) thay vì các phần khác nhau của một bài hát.Web Audio API nối/ghép nối các AudioBuffer khác nhau và phát chúng như một bài hát
Vấn đề là nó vẫn chỉ phát một lần thay vì hai lần và tôi không biết tại sao.
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);
Cảm ơn, nhận xét của bạn đã giúp tôi! http://72lions.github.com/PlayingChunkedMP3-WebAudioAPI/ – 72lions
@cwilso Có thể kết nối nhiều AudioBufferSourceNodes với nhau thành một đĩa đơn tại các thời điểm phát khác nhau không? –
Vâng, bạn chỉ có thể kết nối chúng với cùng một đích. Tôi nghĩ rằng có hiệu quả mà bạn đang tìm kiếm. – cwilso