0.6.4 • Published 7 years ago

react-videoplayer v0.6.4

Weekly downloads
120
License
MIT
Repository
github
Last release
7 years ago

Installation

$ npm install react-videoplayer --save

Getting Started

import VideoPlayer from 'react-videoplayer'
import 'react-videoplayer/lib/index.css'

Features

  • Pass Initial volume, time, playback rate
  • Disable/Enable default controls
  • Disable/Enable custom controls
  • Disable any particular control like playback rate control
  • Pass custom icons for any button
  • Keyboard Controls
  • Customize slider (Overwrite CSS)
  • Control hiding on mouse out or no mouse movement
  • Support for playlist

Upcoming Features (v1)

  • High Order Component one can pass custom Control with exposed API for control
  • Customize keyboard control in run-time

Props

PropTypeDefaultDescription
videoSrcstringvideo url (required)
videoVolumenumber100intial playback volume range 0-100
videoProgressstring0m0svideo start time as string MM:SS
videoPlaybackRatenumber1Default playback rate
autoPlayboolfalsevideo will played as component is mounted
mutedboolfalsemutes the video if true
playButtonImgstringsvg-iconicon path
pauseButtonImgstringsvg-iconicon path
nextButtonImgstringsvg-iconicon path
previousButtonImgstringsvg-iconicon path
volumeButtonImgstringsvg-iconicon path
volumeButtonMuteImgstringsvg-iconicon path
fullScreenButtonImgstringsvg-iconicon path
playbackRateButtonImgstringsvg-iconicon path
previousButtonClassNamestring''class for prev btn is disabled ( when 1st video is played)
nextButtonClassNamestring''class for prev btn is disabled ( when last video is played)
onPlayfuncfunction called when video starts intially
onPlayingfuncfunction called when video played
onEndedfuncfunction called when video ends
playlistboolfalsewhen true prev and next btn are active
playNextfuncfunc called when next buttin is clicked
playPreviousfuncfunc called when prev buttin is clicked
defaultSeekTimenumber10default seek time when video is seeked through key
defaultVolumeChangenumber10default volume change when changed through key
defaultBrowserControlsboolfalsedefault html5 controls
customHtmlControlsbooltruedefault custom controls
keyboardControlsbooltrueenables keyboard controls
notificationClassstring'video-player-notifications'default class for notification
notificationDurationnumber1500timeout for notification

Keyboard Shortcuts

KeyAction
Up ArrowIncrease Volume
Down ArrowDecrease Volume
Right ArrowSeek Forward
Left ArrowSeek Backward
]Increase PlayBack Rate
[Decrease PlayBack Rate
Default Playback Rate
Enter / FFullscreen Toggle
Space / Kplay-Pause Toggle
LSeek Forward
JSeek Backward
MToggle Volume
TToggle TheaterMode
>play Next Video
<play Previous Video
HShow HelpBox

Basic Example

import React from 'react';
import VideoPlayer from 'react-videoplayer'
import 'react-videoplayer/lib/index.css'


class Player extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            videoSrc : 'https://www.w3schools.com/html/mov_bbb.mp4'
        };
        
    }


    render() {
        return (
            <div>
                <VideoPlayer
                    videoSrc={this.state.videoSrc}
                    autoPlay={true}
                />
            </div>
        );
    }
}

export default Player;

Advance Example

This examples contains one more component dragNdrop which is loosely based on react-drag-and-drop through which files are dropped to play.

import React from 'react';
import VideoPlayer from 'react-videoplayer'
import 'react-videoplayer/lib/index.css'
import DragNDropInput from '../components/DragNDropInput';


class Player extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            playlist: [],
            videoFiles: [],
            videoSrc: "",
            currentVideo: -1,
            previousButtonClassName: "",
            nextButtonClassName: ""
        };

        this.DragNDropInputOnDrop = this.DragNDropInputOnDrop.bind(this);
        this.videoPlayerPlayNext = this.videoPlayerPlayNext.bind(this);
        this.videoPlayerPlayPrevious = this.videoPlayerPlayPrevious.bind(this);
    }


    DragNDropInputOnDrop(dragndrop, acceptedFiles) {
        let newVideoFile = [];
        let newPlayList = [];
        let oldVideoFile = this.state.videoFiles;
        let oldPlayList = this.state.playlist;
        acceptedFiles.forEach((file) => {
            const index = helper.isObjectDuplicate(file, oldVideoFile, "name");
            const fileURL = URL.createObjectURL(file);
            if (index === -1) {
                newVideoFile.push(file);
                newPlayList.push(fileURL);
            }
        });

        if (newPlayList.length > 0) {
            this.setState((prevState) => {
                let currentVideo = -1;
                if (prevState.currentVideo === -1) {
                    currentVideo = 0;
                }
                else {
                    currentVideo = prevState.currentVideo;
                }
                const playlist = [...oldPlayList, ...newPlayList];
                const videoFiles = [...oldVideoFile, ...newVideoFile];
                return {
                    nextButtonClassName: "",
                    playlist,
                    videoFiles,
                    currentVideo,
                    videoSrc: playlist[currentVideo]
                };
            });
        }
    }

    videoPlayerPlayNext() {
        if (this.state.currentVideo === this.state.playlist.length - 1) {
            return;
        }
        else {
            this.setState((prevState) => {
                const currentVideo = prevState.currentVideo + 1;
                const className = currentVideo === prevState.playlist.length - 1 ? "disabled" : "";
                return {
                    currentVideo,
                    videoSrc: prevState.playlist[currentVideo],
                    nextButtonClassName: className,
                    previousButtonClassName: ""
                };
            });
        }
    }

    videoPlayerPlayPrevious() {
        if (this.state.currentVideo === 0) {
            return;
        }
        else {
            this.setState((prevState) => {
                const currentVideo = prevState.currentVideo - 1;
                const className = currentVideo === 0 ? "disabled" : "";
                return {
                    currentVideo,
                    videoSrc: prevState.playlist[currentVideo],
                    previousButtonClassName: className,
                    nextButtonClassName: ""
                };
            });
        }
    }


    render() {
        return (
            <div>
                {this.state.videoSrc === "" ? false :
                    <VideoPlayer
                        videoSrc={this.state.videoSrc}
                        playNext={this.videoPlayerPlayNext}
                        playPrevious={this.videoPlayerPlayPrevious}
                        nextButtonClassName={this.state.nextButtonClassName}
                        previousButtonClassName={this.state.previousButtonClassName}
                        autoPlay={true}
                        playlist={this.state.playlist.length > 1}
                    />}
                <DragNDropInput
                    onDrop={this.DragNDropInputOnDrop}
                />

            </div>
        );
    }
}

export default Player;

Screenshots

Note: Image contains music video believer by Imagine Dragons to which I claim not rights.

Issues

Feel free to contribute. Submit a Pull Request or open an issue for further discussion.

License

MIT