Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to use in React? #127

Open
NuttaponTamburanavit opened this issue Dec 24, 2018 · 1 comment
Open

Ability to use in React? #127

NuttaponTamburanavit opened this issue Dec 24, 2018 · 1 comment

Comments

@NuttaponTamburanavit
Copy link

I get error 'getComponent is not function'
when I tried import with videoJs

And I don't want to use global script in html.

Do you have example for React?

@brandonsireland
Copy link

I found a workaround for my usecase.
`

    import React, { Component } from 'react';
    import classes from './VideoPlayer.scss';
    const videojs = require('video.js').default;

    // Styles
    import './videojs-resolution-switcher.css'
    require('!style-loader!css-loader!./videojs-resolution-switcher.css');
    import 'video.js/dist/video-js.css'
    require('!style-loader!css-loader!video.js/dist/video-js.css');

    import demoVideo from '../../assets/demo/cow.mp4'; 

    class VideoPlayer extends Component {
        state = {
            sources: [
                {
                    src: demoVideo,
                    type: 'video/mp4',
                    label: '240p',
                },
                {
                    src: demoVideo,
                    type: 'video/mp4',
                    label: '480p',
                },
                {
                    src: demoVideo,
                    type: 'video/mp4',
                    label: '720p',
                },
                {
                    src: demoVideo,
                    type: 'video/mp4',
                    label: '1080p',
                },
                {
                    src: demoVideo,
                    type: 'video/mp4',
                    label: '2160p',
                }
            ]
        };
        
        componentDidMount() {
            // Instantiate resolution switcher
            if(window) window.videojs = videojs;
            require('./videojs-resolution-switcher')
            
            // instantiate Video.js
            this.player = videojs(
                this.videoNode, {
                    plugins: {
                        videoJsResolutionSwitcher: {
                            default: '480p',
                            dynamicLabel: true
                        },
                    },
                    ...this.props
                },() => {
                    let player = this.player;
                    player.updateSrc(this.state.sources)
                    player.on('resolutionchange', function() {
                        console.info('Source changed to %s', player.src());
                    });
                console.log('onPlayerReady', this)
            });
        };

        componentWillUnmount(){
            if (this.player) {
                this.player.dispose();
            }
        };

        render() {
            return (
                <div className={ classes.VideoPlayer } >
                    <div data-vjs-player>
                        <video
                        {...this.props}
                            ref={ node => this.videoNode = node }
                            src={ demoVideo }
                            className='video-js vjs-16-9 vjs-big-play-centered'>
                        </video> 
                    </div> 
                </div>
            )
        }

    };

    export default VideoPlayer; 

`
just to note that I'm using css-loader as well as style-loader in my Webpack Config for bringing in the CSS. Also I removed the onPlayerReady function from instantiation and just used an anonymous function which actually loaded the plugin correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants