如何使用react-native-video获取视频流?

问题描述 投票:0回答:1

我在 Nodejs 上有后端,代码如下,我使用基于范围的视频流:

router.get('/video/:id', authenticateToken, (req, res) => {
  const range = req.headers.range;
  if (!range) {
    res.status(400).send("Requires Range header");
  }

  // get video stats (about 61MB)
  const videoPath = `uploads/${req.params.id}`;
  const videoSize = fs.statSync(videoPath).size;

  // Parse Range
  // Example: "bytes=32324-"
  const CHUNK_SIZE = 10 ** 6; // 1MB
  const start = Number(range.replace(/\D/g, ""));
  const end = Math.min(start + CHUNK_SIZE, videoSize - 1);

  // Create headers
  const contentLength = end - start + 1;
  const headers = {
    "Content-Range": `bytes ${start}-${end}/${videoSize}`,
    "Accept-Ranges": "bytes",
    "Content-Length": contentLength,
    "Content-Type": "video/mp4",
  };

  // HTTP Status 206 for Partial Content
  res.writeHead(206, headers);

  // create video read stream for this particular chunk
  const videoStream = fs.createReadStream(videoPath, { start, end });

  // Stream the video chunk to the client
  videoStream.pipe(res);
});

但是react-native-video不会发送任何范围标头,当我尝试从服务器获取mp4视频时,我收到错误:

{"error": {"extra": -2147483648, "what": 1}}

使用react-native-video包:

<Video
  source={{
    uri: `http://*server ip*/video/${exercise.content}`,
    type: 'mp4',
  }}
/>

我应该用什么来代替?

node.js react-native express video-streaming
1个回答
0
投票

表达静态解决我的问题。感谢@SilvanBregy

app.use('/video', express.static('uploads'))
© www.soinside.com 2019 - 2024. All rights reserved.