-
Notifications
You must be signed in to change notification settings - Fork 176
Open
Description
I am using Next.js and have integrated Player.js with it. This setup is causing an issue when I try to seek to a particular timestamp. Sometimes it navigates to that specific timestamp, but other times it resets the time to 0 whenever I refresh the page.
So, I tried putting the 'ready' function inside a setTimeout function. Now, it's working okay, but it's not an appropriate approach.
Maybe the issue is with the trigger of the ready function because it seems unable to perform the setCurrentTime function.
Here, is the code I am using:
import React, { useEffect, useRef, useState } from 'react'
import playerjs from 'player.js'
interface TimeUpdateProps {
seconds: number
duration: number
}
interface PlayerComponentSpecificProps {
id: string
onTimeUpdate?: (data: TimeUpdateProps) => void
resumeTime?: number
}
type PlayerComponentProps = PlayerComponentSpecificProps &
React.IframeHTMLAttributes<HTMLIFrameElement>
const PlayerComponent: React.FC<PlayerComponentProps> = ({
id,
onTimeUpdate,
resumeTime,
...iframeProps
}) => {
const playerRef = useRef<HTMLIFrameElement>(null)
useEffect(() => {
if (playerRef.current) {
const newPlayer = new playerjs.Player(playerRef.current)
const handleReady = () => {
console.log('Player is ready', resumeTime)
setTimeout(() => {
if (resumeTime) {
newPlayer.setCurrentTime(resumeTime)
}
}, 1000)
newPlayer.setCurrentTime(resumeTime)
}
newPlayer.on('ready', handleReady)
newPlayer.on('timeupdate', (data: TimeUpdateProps) => {
console.log('timeupdate', JSON.stringify(data))
if (onTimeUpdate) {
onTimeUpdate(data)
}
})
return () => {
newPlayer.off('ready', handleReady)
}
} }
}, [id])
return (
<div>
{resumeTime}
<iframe ref={playerRef} id={id} {...iframeProps} />
</div>
)
}
export default PlayerComponent
Metadata
Metadata
Assignees
Labels
No labels