Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

สตรีมไฟล์วิดีโอไปยัง HTML5 video player ด้วย Node.js ให้ควบคุมวิดีโอทำงานต่อ


ใช้ createReadStream ส่งส่วนที่ร้องขอไปยังไคลเอนต์ การเรียกฟังก์ชัน createReadStream() จะให้ readable stream

ตัวอย่าง

โค้ดดังนี้:

stream = fs.createReadStream(path);
stream.on('open', function () {
  res.writeHead(206,{
    "Content-Range":"bytes " + begin + "-" + end + "/" + total,
    "Accept-Ranges":"bytes",
    "Content-Length":chunksize,
    "Content-Type":"video/mp4"
  });
  stream.pipe(res);
});