视频结束后如何使用react-player在视频上显示按钮
How to display a button on the video after video ends with react-player
在我的 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}
/>
您可以在视频结束时设置布尔状态值 (onEnded
) 并有条件地显示 "next" 按钮。
该按钮需要绝对定位以覆盖视频。将按钮弹性框居中是众多选项之一。
以下代码也可作为代码沙箱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>
)
}
在我的 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}
/>
您可以在视频结束时设置布尔状态值 (onEnded
) 并有条件地显示 "next" 按钮。
该按钮需要绝对定位以覆盖视频。将按钮弹性框居中是众多选项之一。
以下代码也可作为代码沙箱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>
)
}