根据此 library 文档,我正在尝试使用 subarray()
函数,但我总是收到此错误 Uncaught (in promise) TypeError: audioBuffer.subarray is not a function
这是我用来生成与库一起使用的 arrayBuffer 的代码。我也尝试过创建 Int16Array
但没有成功。
const createChunksFromStream = (details) => {
if(details.statusCode === 206){
console.log(details);
const headers = new Headers();
details.responseHeaders.forEach( (header) => {
headers.append(header.name, header.value);
});
const request = new Request(details.url, {method: 'GET', headers: headers})
fetch(request)
.then( (res) => res.arrayBuffer() )
.then( (res) => {
console.log(res);
let buffer = new Uint8Array(res);
blobChunks.push(buffer);
});
}
}
const processChunks = async (blobChunks) => {
let mp3encoder = new lamejs.Mp3Encoder(1, 44100, 128);
console.log(mp3encoder);
console.log('chunk stream log', blobChunks);
let audioBuffer;
blobChunks.forEach( (item) => {
audioBuffer = [...item];
});
console.log(audioBuffer);
sampleBlockSize = 1152;
let mp3Data = [];
for (let i = 0; i < audioBuffer.length; i += sampleBlockSize) {
sampleChunk = samples.subarray(i, i + sampleBlockSize);
let mp3buffer = mp3encoder.encodeBuffer(sampleChunk);
if (mp3buffer.length > 0) {
mp3Data.push(mp3buffer);
}
}
// see if there's any data left
mp3buffer = mp3encoder.flush();
if(mp3buffer.length > 0){
mp3Data.push(mp3buffer);
}
console.log('processed stream as mp3', mp3Data);
let output = new Blob([mp3Data], {type: 'audio/mp3'});
let fileURL = URL.createObjectURL(output);
chrome.downloads.download({
saveAs: true,
url: fileURL
}, (downloadId) => {
console.log(downloadId);
URL.revokeObjectURL(fileURL);
});
}
我该如何解决这个问题,是否有任何错误或将缓冲区保存在音频文件中的替代方法?