簡體   English   中英

服務器停止時 chrome 不播放視頻文件,而 firefox 瀏覽器可以播放

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

我在 chrome 中看到了一種行為,即當向服務器發送請求並且瀏覽器獲取媒體數據時。 如果服務器出現故障,chrome 將無法播放媒體(盡管它下載了大小為 168 MB 的整個媒體,如下圖所示),但 firefox 即使在離線時也能夠播放整個媒體文件。

chrome.network 選項卡日志

<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>

我的 node.js 代碼:

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

我認為上面的 pipe () 方法將 stream 分塊,但不知何故 firefox 能夠獲取所有數據。 我如何防止 firefox 讓所有這些數據離線可用,而 stream 只能以塊的形式提供。

將響應標頭更新為以下修復了該問題。 我認為不同之處在於傳遞了“接受范圍”header 和res.writeHead(206, headers); .

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM