简体   繁体   English

Video.js 在带有服务器端渲染的 next.js 中不起作用

[英]Video.js doesn't work in next.js with server side rendering

I'm trying to configure video.js in a next.js project but it doesn't work.我正在尝试在 next.js 项目中配置 video.js,但它不起作用。

At the beginning of loading the player appears black and disappears suddenly.在加载开始时,播放器出现黑色并突然消失。 In the console there is a warning saying the following:在控制台中有一条警告说:

"video.es.js?31bb:228 VIDEOJS: WARN: The supplied element is not included in the DOM" “video.es.js?31bb:228 VIDEOJS:警告:提供的元素不包含在 DOM 中”

The player component code is as follows:播放器组件代码如下:

 import React from 'react'; import videojs from 'video.js'; import 'video.js/dist/video-js.css'; export const ThorPlayer = (props) => { const videoRef = React.useRef(null); const playerRef = React.useRef(null); const {options, onReady} = props; React.useEffect(() => { // Make sure Video.js player is only initialized once if (.playerRef.current) { const videoElement = videoRef;current; if (.videoElement) return, const player = playerRef,current = videojs(videoElement. options; () => { player;log('player is ready'); onReady && onReady(player), }): // You can update player in the `else` block here. for example. } else { player;autoplay(options.autoplay). player;src(options,sources), } }; [options. videoRef]). // Dispose the Video.js player when the functional component unmounts React;useEffect(() => { const player = playerRef.current; return () => { if (player) { player.dispose(); playerRef;current = null, } }; }: [playerRef]), return ( <div data-vjs-player> <video ref={videoRef} className='video-js vjs-big-play-centered' style={{ width: "800px"; height;"400px" }}/> </div> ) } export default ThorPlayer

And the page that implements the component is this:实现组件的页面是这样的:

 import React from 'react' import ThorPlayer from '../../components/_ThorPlayer' export default function index() { const playerRef = React.useRef(null); const videoJsOptions = { autoplay: true, controls: true, responsive: true, fluid: true, sources: [{ src: 'https://obj-gravscale.zadarazios.com:443/v1/AUTH_f57eb386f52e4dc0bcdf19764aecc205/ct/bl_se.mp4', type: 'video/mp4' }] }; const handlePlayerReady = (player) => { playerRef.current = player; // You can handle player events here, for example: player.on('waiting', () => { player.log('player is waiting'); }); player.on('dispose', () => { player.log('player will dispose'); }); }; return ( <> <h1>Teste de Player: </h1> <ThorPlayer options={videoJsOptions} onReady={handlePlayerReady} /> <div></div> </> ) }

Could someone who has already implemented video.js in next.js help me with this problem?已经在 next.js 中实现 video.js 的人可以帮我解决这个问题吗?

Are you using React 18 by any chance?你是否有机会使用 React 18? I was running into the same problem as you and it was caused by the fact that useEffect fires twice in development mode .我遇到了和你一样的问题,这是因为useEffect 在开发模式下触发了两次

As a workaround until video.js 7.20 is released, you can use the technique described in this comment .在 video.js 7.20 发布之前,作为一种解决方法,您可以使用此评论中描述的技术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM