首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react-player在视频结束后在视频上显示按钮

如何使用react-player在视频结束后在视频上显示按钮
EN

Stack Overflow用户
提问于 2020-04-01 22:42:04
回答 1查看 968关注 0票数 1

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

代码语言:javascript
复制
 <ReactPlayer
          url={videoPath}
          width="100%"
          height='100%'
          controls={true}
          className="player"
          onEnded={markLessonAsCompleted}
          config={{
            file: {
              attributes: {
                controlsList: "nodownload"
              }
            }
          }}
          volume={1}
        />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-08 20:08:26

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

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

代码语言:javascript
复制
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>
  )
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60973531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档