我正在通过Express服务器将从Gridfs存储桶中检索到的音频分流传输到客户端的html音频元素,直到它们可用。 Edge和Firefox都下载并缓冲歌曲,并允许我查找缓冲的歌曲,而chrome似乎不缓冲它,而是顺序下载块(我可能错了)。
trackRoute.get('/:trackID', (req, res) => {
try{
var trackID = new ObjectID(req.params.trackID);
}
catch(err){
return res.status(400).json({ message:" :Invalid trackID in URL parameter. Must be a single String of 12 bytes or a string of 24 hex characters" });
}
res.set('content-type', 'audio/mp3');
res.set('accept-ranges', 'bytes');
res.set('Transfer-Encoding', 'chunked');
let bucket = new mongodb.GridFSBucket(db,
{
bucketName: 'songs'
}
);
let downloadStream = bucket.openDownloadStream(trackID);
// downloadStream.pipe(res);
downloadStream.on('data', (chunk) => {
res.write(chunk);
});
downloadStream.on('error', (err) => {
res.sendStatus(404);
});
downloadStream.on('end', () => {
res.end();
});
// let readableSongStream = fs.createReadStream("C:\\Users\\Sajeel\\Music\\Demons - Imagine Dragons.mp3");
// readableSongStream.pipe(res);
});
Taking 2.3 mins and still wont let me seek the song until whole song is already played