简体   繁体   中英

chrome doesn't play the video file when the server stops while firefox browser can play

I have seen a behaviour in chrome which is when a request is sent to the server and browser gets the media data. If the server goes down chrome is not able to play the media(though it downloaded the entire media of size 168 MB as per.network tab with image shown below) but firefox was able to play the entire media file even while being offline.

chrome.network tab log

<html>
<head>
    <h1> Video Player</h1>
  <script>

  </script>

</head>
<body>
    <video id="videoPlayer" width="800" height="400" controls> 
         <source src="/video" type="video/mp4">
            Your browser does not support the video tag.
    </video>
</body>

My node.js code:

  const headers = {
    'Content-Length': fileSize,
    'Content-Type': 'video/mp4',
  }
  res.writeHead(200, headers)
  fs.createReadStream(videoFilePath).pipe(res)

I thought.pipe () method above will stream in chunks but somehow firefox was able to get all of the data. How do i prevent firefox to have all of this data available offline and stream only in chunks.

Updating the response headers to below fixed the issue. I think the difference was passing the "accept-ranges" header with partial response of res.writeHead(206, headers); .

  headers = { 
    "Accept-Ranges": "bytes", 
    "Content-Length": totalFileSize, 
    "Content-Type": 'video/mp4' 
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM