reactjs - 如何将 VideoJS 插件与 ReactJS 一起使用?

标签 reactjs html5-video video.js

我正在尝试使用videojs-offset插件,使用 ReactJS。

但是,它向我显示了以下错误 -

TypeError: this.player.offset is not a function

import React, { Component } from 'react';
import './App.css';
import PropTypes from 'prop-types';
import "video.js/dist/video-js.css";
import videojs from 'video.js'
import videoJsContribHls from 'videojs-contrib-hls'
import offset from 'videojs-offset';

export default class VideoPlayer extends React.Component {
    componentDidMount() {
        // instantiate Video.js
        this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
            console.log('onPlayerReady', this);
            this.player.offset({
                start: 10,
                end: 300,
                restart_beginning: false //Should the video go to the beginning when it ends
          });
        });
        }

    // destroy player on unmount
    componentWillUnmount() {
        if (this.player) {
            this.player.dispose()
        }
    }
    render() {
        return (
            <div>
                <div data-vjs-player>
                    <video ref={ node => this.videoNode = node } className="video-js"></video>
                </div>
            </div>
        )
    }
}

这是将任何 videojs 插件与 ReactJS 集成时的常见问题。

最佳答案

通过以下更改解决了此问题 -

首先,在VideoJS选项中,应该初始化插件,例如, 插件:偏移:{开始:10,结束:300,restart_beginning:false}}

如果您的插件没有任何参数,您可以像 plugin_name: {} 一样初始化它

然后,在 componentDidMount() 方法中,像这样注册插件 -

componentDidMount() {
        videojs.registerPlugin('offset',offset);
        this.player = videojs(this.videoNode, this.props, function 
        onPlayerReady() {
            console.log('onPlayerReady', this);
        });}

此解决方案适用于将任何 videojs 插件与 React 集成。

关于reactjs - 如何将 VideoJS 插件与 ReactJS 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965959/

相关文章:

javascript - 如何删除事件监听器,以便在卸载组件时它不会尝试更新状态?

javascript - React 控制内容可编辑

node.js - 在react和node js应用程序中使用socket.io

javascript - 如何在全屏模式下检测浏览器中的 YouTube 视频变化

html - IE9 不显示 mp4 视频?

javascript - 有没有办法在用户登录的情况下仅播放 HTML5 视频?

javascript - class 和 className 在 css 继承方面有什么区别?

javascript - 在 Flash 模式下切换比特率后,VideoJS 分辨率切换器不会自动重新启动

javascript - 数据属性未添加到创建的元素

javascript - 将 video.js 库与 Webpack 的 ProvidePlugin 结合使用