Coder Social home page Coder Social logo

zzarcon / react-video-renderer Goto Github PK

View Code? Open in Web Editor NEW
116.0 8.0 14.0 1.92 MB

Build custom video players effortless

Home Page: https://zzarcon.github.io/react-video-renderer

License: MIT License

TypeScript 99.39% HTML 0.61%
react component video renderer render-props custom player video-player render-prop

react-video-renderer's Introduction

react-video-renderer Build Status

Build custom video players effortless

  • Render props, get all video state passed down as props.
  • Bidirectional flow to render and update the video state in a declarative way.
  • No side effects out of the box, you just need to build the UI.
  • Actions handling: play, pause, mute, unmute, navigate, etc
  • Dependency free, <2KB size
  • Cross-browser support, no more browser hacks.

Demo ๐ŸŽฉ

https://zzarcon.github.io/react-video-renderer

Installation ๐Ÿš€

yarn add react-video-renderer

Usage โ›

Render video state and communicate user interactions up when volume or time changes.

import Video from 'react-video-renderer';

<Video src="https://mysite.com/video.mp4">
  {(video, state, actions) => (
    <div>
      {video}
      <div>{state.currentTime} / {state.duration} / {state.buffered}</div>
      <progress value={state.currentTime} max={state.duration} onChange={actions.navigate} />
      <progress value={state.volume} max={1} onChange={actions.setVolume} />
      <button onClick={actions.play}>Play</button>
      <button onClick={actions.pause}>Pause</button>
      <button onClick={actions.requestFullScreen}>Fullscreen</button>
    </div>
  )}
</Video>
Logo

Api ๐Ÿ’…

Props

interface Props {
  src: string;
  children: RenderCallback;
  controls?: boolean;
  autoPlay?: boolean;
  preload?: string;
  textTracks?: VideoTextTracks;
}

Render method

type RenderCallback = (reactElement: ReactElement<HTMLMediaElement>, state: VideoState, actions: VideoActions, ref: React.RefObject<HTMLMediaElement>) => ReactNode;

State

interface VideoState {
  status: 'playing' | 'paused' | 'errored';
  currentTime: number;
  currentActiveCues: (kind: VideoTextTrackKind, lang: string) => TextTrackCueList | null | undefined;
  volume: number;
  duration: number;
  buffered: number;
  isMuted: boolean;
  isLoading: boolean;
  error?: MediaError | null;
}

Actions

interface VideoActions {
  play: () => void;
  pause: () => void;
  navigate: (time: number) => void;
  setVolume: (volume: number) => void;
  requestFullscreen: () => void;
  mute: () => void;
  unmute: () => void;
  toggleMute: () => void;
}

Error handling ๐Ÿ’ฅ

this is all you need to detect video errors

<Video src="some-error-video.mov">
  {(video, state) => {
    if (state.status === 'errored') {
      return (
        <ErrorWrapper>
          Error
        </ErrorWrapper>
      );
    }

    return (
      <div>
        {video}
      </div>
    )
  }}
</Video>

Loading state โœจ

you can still interact with the player regardless if the video is loading or not

<Video src="my-video.mp4">
  {(video, state, actions) => {
    const loadingComponent = state.isLoading ? 'loading...' : undefined;

    return (
      <div>
        {video}
        {loadingComponent}
        <button onClick={actions.play}>Play</button>
        <button onClick={actions.pause}>Pause</button>
      </div>
    )
  }}
</Video>

Video text tracks ๐Ÿš‚

HTML5 text tracks support for videos.

subtitles can be rendered natively, or they can be rendered using VideoState.currentActiveCues property:

<Video 
  src="my-video.mp4"
  textTracks={{
    'subtitles': {
      selectedTrackIndex: 0,
      tracks: [
        { src: 'subtitles-en.vtt', lang: 'en', label: 'Subtitles (english)' },
        { src: 'subtitles-es.vtt', lang: 'es', label: 'Subtitles (spanish)' },
      ]
    }
  }}
>
  {(video, state, actions) => {
    const cues = state.currentActiveCues('subtitles', 'en');
    const subtitles =
      cue && cue.length > 0 ? (
        <div>
          {Array.prototype.map.call(cues, (cue, i) => <span key={i}>{cue.text}</span>)}
        </div>
      ) : undefined;

    return (
      <div>
        {video}
        {subtitles}
        <button onClick={actions.play}>Play</button>
        <button onClick={actions.pause}>Pause</button>
      </div>
    )
  }}
</Video>

Author ๐Ÿง”

@zzarcon

react-video-renderer's People

Contributors

abhayani123 avatar achamas-atlassian avatar d4rkr00t avatar digitalkaoz avatar dragonworx avatar fghilini avatar fredguile avatar soswow avatar vvvlasov avatar zzarcon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-video-renderer's Issues

Default currentTime from props

Defining a starting time is a pretty common scenario, but couldn't be done currently. Also, I couldn't find a way to subscribe to video state changes, so actions could be triggered from outside the player itself.

Not sure if that should be part of an API improvement o improving state using props for default values.

Api

Interfaces

interface VideoRendererProps {
  src: string;
  controls?: boolean;
  preload?: 'metadata' | '';
  poster?: 'string';
}

interface VideoState {
  status: 'playing' | 'paused';
  currentTime: number;
  volume: number;
}

Usage

import Video from 'react-video-renderer';

<Video src="" controls preload="metadata" >
  {(element: HTMLVideoElement, state: VideoState) => {

  }}
</Video>

Error in method name

Fullscreen

This is mistake - actions.requestFullScreen
Must be - actions.requestFullscreen

Add props for muted and playsInline

The video props do not support playsInline or muted which is problematic for autoplay videos and iphones. I am trying to submit a PR but I can't test since I am blocked by #36

Hide controls on fullScreen mode

Should this component take of hiding the native controls when controls={false} on full screen mode?

video::-webkit-media-controls {
  display:none !important;
}

Unable to use callbacks

<Video src={src} onPlay={onPlay}>
        {VideoRenderCallback}
</Video>

Unable to do this since onPlay is an invalid prop. Looking at the source I realize this is the case for all such methods. Can we expose these methods?

I'd be happy to open a PR if you think this is a valid usecase.

Use SyntheticEvent type for event listeners

Instead of:

onVolumeChange = (e: any) => {
    const video = e.target as HTMLVideoElement;

We can do:

import {SyntheticEvent} from 'react';

onVolumeChange = (e: SyntheticEvent<HTMLVideoElement>) => {
    const video = e.currentTarget;

Supports for HLS

Hi, I believe the current video renderer doesn't support HLS format, is there a way to extend the library with HLS?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.