【问题标题】:How to display a button on the video after video ends with react-player视频以react-player结束后如何在视频上显示按钮
【发布时间】:2020-07-13 08:36:56
【问题描述】:

在我的 React 应用程序中,我想要一个按钮来导航到将在当前视频播放完毕后出现在视频中的下一课。我正在使用react-player 来显示视频。我想知道我怎样才能做到这一点是react-player。非常感谢任何有用的提示。

 <ReactPlayer
          url={videoPath}
          width="100%"
          height='100%'
          controls={true}
          className="player"
          onEnded={markLessonAsCompleted}
          config={{
            file: {
              attributes: {
                controlsList: "nodownload"
              }
            }
          }}
          volume={1}
        />

【问题讨论】:

    标签: reactjs react-player


    【解决方案1】:

    您可以在视频结束时设置布尔状态值 (onEnded) 并有条件地显示“下一步”按钮。 该按钮需要绝对定位以覆盖视频。将按钮弹性框居中是众多选项之一。

    以下代码也可用于here 作为代码沙箱。

    function App() {
      const urls = [
        'https://www.youtube.com/watch?v=oPZXk4ESdng?t=50',
        'https://www.youtube.com/watch?v=bcWZ6C-kn_Y'
      ]
      const [currentUrlIndex, setCurrentUrlIndex] = React.useState(0)
      const [showNextButton, setShowNextButton] = React.useState(false)
    
      return (
        <div
          style={{
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center'
          }}
        >
          <ReactPlayer
            url={urls[currentUrlIndex]}
            playing
            controls
            onEnded={() => setShowNextButton(true)}
            style={{
              width: '100%',
              height: '100%'
            }}
          />
          {showNextButton && (
            <button
              onClick={() => {
                setCurrentUrlIndex(
                  prevUrlIndex => (prevUrlIndex + 1) % urls.length
                )
                setShowNextButton(false)
              }}
              style={{
                position: 'absolute',
                zIndex: 10,
                fontSize: '2em'
              }}
            >
              next
            </button>
          )}
        </div>
      )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2021-08-07
      • 2021-02-17
      • 2014-06-01
      相关资源
      最近更新 更多