加载本地 mp4 文件作为 React Gatsby 网站中视频标签的源

IT技术 reactjs video gatsby
2021-05-10 06:49:49

目标: 我正在尝试在 React gatsby 站点上无限循环播放视频。我在资产下本地有一个 mp4 文件,但它没有显示在我的屏幕上。

我试过的: 我认为这可能是 CSS 问题,所以我设置了它,它显然是窗口的 100% 宽度和高度,但视频仍然没有显示。

JS:

import React from "react"
import "./index.css"
export default () => (
  <div>
    <video id="background-video" loop autoPlay>
      <source src="..\assets\video\Cenote_Squad_Cinemagraph_1500x840_Final.mp4" type="video/mp4"/>
      Your browser does not support the video tag.
    </video>
  </div>
)

CSS:

#background-video {
    height: 100%;
    width: 100%;
    float: left;
    top: 0;
    padding: none;
    position: fixed;
    /* optional depending on what you want to do in your app */
}
2个回答

我正在使用此配置:

import forest from "../../images/forest.mp4"
//import the video file and then 

<video width="100%" height="100vh"  preload='auto' poster={poster} loop autoPlay muted>
 <source src={video} type="video/mp4" />
 Your browser does not support HTML5 video.
</video>

原来你需要使用

require("..\assets\video\Cenote_Squad_Cinemagraph_1500x840_Final.mp4")

作为源