我在我的next.js网站上使用react-player,在使用seekTo函数时遇到问题。它导致错误消息:“playerRef.seekTo不是函数”
我也尝试了“playerRef.current.seekTo(parseFloat(e.target.value))”,但发生了同样的错误。
我的代码是基于react-player演示库:https://github.com/cookpete/react-player/blob/master/src/demo/App.js
import { useRef } from "react";
const ReactPlayer = dynamic(() => import("react-player"), { ssr: false });
const Player = ({url}) => {
const playerRef = useRef();
const [player, setPlayer] = useState({
playing: true,
muted: true,
played: 0,
loaded: 0,
duration: 0,
seeking:false
});
const handlePlayPause = function () {
setPlayer({ ...player, playing: !player.playing });
};
const handleMuted = () => {
setPlayer({ ...player, muted: !player.muted });
};
const handleSeekMouseDown = (e) => {
setPlayer({ ...player, seeking: true });
};
const handleSeekChange = (e) => {
setPlayer({ ...player, played: parseFloat(e.target.value) });
};
const handleSeekMouseUp = (e) => {
setPlayer({ ...player, seeking: false });
playerRef.seekTo(parseFloat(e.target.value));
};
const handleProgress = (state) => {
if (!player.seeking) {
setPlayer(state);
}
};
return (
<div
className={`relative justify-center content-center w-full`}
>
<ReactPlayer
ref={playerRef}
className="absolute rounded"
width="100%"
height="100%"
url={url}
volume={1}
playIcon={<></>}
playing={player.playing}
loop={true}
muted={player.muted}
onProgress={handleProgress}
></ReactPlayer>
<div
className={"absolute w-full h-full"}
onClick={handlePlayPause}
></div>
<div>
<input
type="range"
min={0}
max={0.999999}
step="any"
value={player.played}
onMouseDown={handleSeekMouseDown}
onChange={handleSeekChange}
onMouseUp={handleSeekMouseUp}
/>
</div>
</div>
);
};
export default Player;
谢谢
1条答案
按热度按时间jyztefdp1#
1.创建另一个组件,并将ref和其他属性传递到此组件,然后手动导入ReactPlayer
创建虚拟播放器.jsx ==〉
导出默认的VPlayer;
1.动态导入组件并将属性传递给它
并在其他组件或页面中使用它==〉