首页 > 解决方案 > 使用 React 使 YouTube 视频自动播放

问题描述

我正在尝试使用 react 让我的视频自动播放。添加autoplay=1为参数不起作用。有什么建议吗?

这是我的代码。

<div
  className="video mt-5"
  style={{
         position: "relative",
         paddingBottom: "56.25%" /* 16:9 */,
         paddingTop: 25,
         height: 0
       }}
>
    <iframe
       style={{
             position: "absolute",
             top: 0,
             left: 0,
             width: "100%",
             height: "100%"
             }}
        src={'https://www.youtube.com/embed/hEnr6Ewpu_U?'}
        frameBorder="0"
    />
</div>

标签: javascripthtmlcssreactjsyoutube

解决方案


我不会使用<iframe>因为当你去 Youtube 时你仍然需要点击才能播放。我会使用一个名为 react-player 的库

import ReactPlayer from "react-player";

<ReactPlayer
  url={props.url}
  playing={true}
  width={props.width}
  height={props.height}
/>

它会自动播放:)


推荐阅读