• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
    JavaScript
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React Component API for JW Player

react-jw-player πŸŽ₯ Build Status

<ReactJWPlayer> is a React Component for initializing client-side instances of JW Player. Simply give <ReactJWPlayer> the id of your player script, and the id of a JW Player video or playlist. The component comes with several event hooks that can be accessed through component props.

Contents

Installation

npm install react-jw-player

Usage

At the mininum, you can just use something like the three following code snippets:

Playing a JW Player JSON Playlist

import React from 'react';
import ReactDOM from 'react-dom';
import ReactJWPlayer from 'react-jw-player';

ReactDOM.render(
  <ReactJWPlayer
    playerId='my-unique-id'
    playerScript='https://link-to-my-jw-player/script.js'
    playlist='https://link-to-my-playlist.json'
  />,
  document.getElementById('my-root-div');
);

Playing a custom Playlist

import React from 'react';
import ReactDOM from 'react-dom';
import ReactJWPlayer from 'react-jw-player';

const playlist = [{
  file: 'https://link-to-my-video.mp4',
  image: 'https://link-to-my-poster.jpg',
  tracks: [{
    file: 'https://link-to-subtitles.vtt',
    label: 'English',
    kind: 'captions',
    'default': true
  }],
},
{
  file: 'https://link-to-my-other-video.mp4',
  image: 'https://link-to-my-other-poster.jpg',
}];

ReactDOM.render(
  <ReactJWPlayer
    playerId='my-unique-id'
    playerScript='https://link-to-my-jw-player/script.js'
    playlist={playlist}
  />,
  document.getElementById('my-root-div');
);

Playing a Specific File

import React from 'react';
import ReactDOM from 'react-dom';
import ReactJWPlayer from 'react-jw-player';

ReactDOM.render(
  <ReactJWPlayer
    playerId='my-unique-id'
    playerScript='https://link-to-my-jw-player/script.js'
    file='https://link-to-my-video.mp4'
  />,
  document.getElementById('my-root-div');
);

For more complex interaction, check out the container component example here

To generate preroll, supply the player with the generatePrerollUrl prop. This prop just needs to be a function that returns a valid VAST tag! See Optional Configuration Props for more info.

Required Props

These are props that modify the basic behavior of the component.

  • playerId
    • A unique Id for the player instance. Used to distinguish the container divs.
    • Type: string
    • Example: playerId="my-jw-player-instance"
  • playerScript
    • Link to a valid JW Player script.
    • Type: string
    • Example: https://content.jwplatform.com/libraries/abCD1234.js
  • playlist OR file
    • Link to a valid JW Player playlist or video file, or playlist array. Cool tip: JW Player automatically generates JSON feeds for individual videos if you use the video id in place of abCD1234. You can use this to get meta data on the videos without loading an actual playlist.
    • Type: string (for file and playlist) or array (for playlist)
    • Example: https//content.jwplatform.com/feeds/abCD1234.json

Optional Configuration Props

  • aspectRatio
    • An optional aspect ratio to give the video player. Can be 'inherit', 1:1 or 16:9 currently.
    • Defaults to 'inherit'.
  • className
    • An optional class name to give the container div.
    • Type: string
  • customProps
    • An optional object containing properties to be applied directly to the JW Player instance. Add anything in the API, like skins, into this object. customProps={{ skin: { name: 'six' } }}.
    • Type: object
  • isAutoPlay
    • Determines whether the player starts automatically or not.
    • Type: boolean
  • isMuted
    • Determines whether the player starts muted or not.
    • Type: boolean
  • generatePrerollUrl(video)
    • Supply a function that returns a VAST or GOOGIMA tag for use in generating a preroll advertisement.
    • Arguments:
      • video
        • This is a video object for the current item loaded in the player. You can use it to help generate your preroll tags.
  • image
    • URL to a poster image to display before playback starts
    • Type: string
  • licenseKey
    • License Key as supplied in the jwplayer dashboard, under: Players > Tools > Downloads > JW Player X (Self-Hosted)
    • Type: string
  • useMultiplePlayerScripts
    • EXPERIMENTAL - Allows you to load multiple player scripts and still load the proper configuration. Expect bugs, but report them!
    • Type: boolean

Event Hooks

react-jw-player dynamically supports all events in JW Player. Simply preface the event name with on and pass it in as a prop.

Examples:

  • ready => onReady
  • setupError => onSetupError

react-jw-player has layered some different functionality on some of these events, so please check the docs below if you find any unexpected behavior!

Optional Advertising Event Hook Props

  • onAdPause(event)
    • A function that is run when the user pauses the preroll advertisement.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onAdPlay(event)
    • A function that is run once, when the preroll advertisement first starts to play.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onAdResume(event)
    • A function that is run when the user resumes playing the preroll advertisement.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onAdSkipped(event)
    • A function that is run when the user skips an advertisement.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onAdComplete(event)
    • A function that is run when an ad has finished playing.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.

Optional Player Event Hook Props

  • onAutoStart(event)
    • A function that is run once, when an autoplaying player starts to play a video.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onEnterFullScreen(event)
    • A function that is run when the user fullscreens a video.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onError(event)
    • A function that is run when the player errors.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onExitFullScreen(event)
    • A function that is run when the user un-fullscreens a video.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onMute(event)
    • A function that is run when the user mutes the player.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onPause(event)
    • A function that is run when the user pauses the player during a video.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onPlay(event)
    • A function that is run when a video first starts to play.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onReady(event)
    • A function that is run once when the video player is ready.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onResume(event)
    • A function that is run when the user plays a video after pausing it.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onSetupError(event)
    • A function that is run when the player errors during setup.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onTime(event)
    • A function that is run whenever the playback position gets updated.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onUnmute(event)
    • A function that is run when the user unmutes the player.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onVideoLoad(event)
    • A function that is run whenever a new video is loaded into the player.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.

Optional Time Event Hook Props

  • onThreeSeconds(event)
    • A function that is run when the playhead reaches passed the 3 second mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onTenSeconds(event)
    • A function that is run when the playhead reaches passed the 10 second mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onThirtySeconds(event)
    • A function that is run when the playhead reaches passed the 30 second mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onTwentyFivePercent(event)
    • A function that is run when the playhead reaches passed the 25% mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onFiftyPercent(event)
    • A function that is run when the playhead reaches passed the 50% mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onSeventyFivePercent(event)
    • A function that is run when the playhead reaches passed the 75% mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onNinetyFivePercent(event)
    • A function that is run when the playhead reaches passed the 95% mark.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.
  • onOneHundredPercent(event)
    • A function that is run when the a video ends.
    • Type: function
    • Arguments:
      • event
        • This is the event object passed back from JW Player itself.

Example Container Component

import React from 'react';
import PropTypes from 'prop-types';

import ReactJWPlayer from 'react-jw-player';

const displayName = 'ReactJWPlayerContainer';

const propTypes = {
  playlist: PropTypes.string.isRequired,
  playerScript: PropTypes.string.isRequired
};

class ReactJWPlayerContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      videoTitle: '',
    };

    this.onAdPlay = this.onAdPlay.bind(this);
    this.onReady = this.onReady.bind(this);
    this.onVideoLoad = this.onVideoLoad.bind(this);

    // each instance of <ReactJWPlayer> needs a unique id.
    // we randomly generate it here and assign to the container instance.
    this.playerId = someFunctionToRandomlyGenerateId();
  }
  onReady(event) {
    // interact with JW Player API here
    const player = window.jwplayer(this.playerId);
  }
  onAdPlay(event) {
    // track the ad play here
  }
  onVideoLoad(event) {
    this.setState({
      videoTitle: event.item.description // this only works with json feeds!
    });
  }
  render() {
    return (
      <div className='react-jw-player-container'>
        <h1>{ this.state.videoTitle }</h1>
        <ReactJWPlayer
          playlist={this.props.playlist}
          licenseKey='your-license-key'
          onAdPlay={this.onAdPlay}
          onReady={this.onReady}
          onVideoLoad={this.onVideoLoad}
          playerId={this.playerId} // bring in the randomly generated playerId
          playerScript='https://link-to-your-jw-player-script.js'
        />
      </div>
    );
  }
}

ReactJWPlayerContainer.propTypes = propTypes;
ReactJWPlayerContainer.defaultProps = defaultProps;
ReactJWPlayerContainer.displayName = displayName;
export default ReactJWPlayerContainer;

Contributing

Just do it!

shia

More Repositories

1

distro

Transform article HTML for Facebook Instant Articles, Apple News and Google AMP
HTML
97
star
2

html-to-article-json

Parses & normalizes html to a well-structured & easy to use article json format
JavaScript
55
star
3

story-json

Story JSON document format
JavaScript
41
star
4

apple-news

A Node.js client for interacting with the Apple News API πŸ“°
JavaScript
37
star
5

html-to-amp

JavaScript
35
star
6

css-path

Get the unique css-path to a DOM-element.
JavaScript
31
star
7

html-word-count

Counts words in HTML body
JavaScript
26
star
8

story-json-to-amp

Compile story-json documents into AMP stories
JavaScript
25
star
9

courier

Create email components with React / react-html-email and send them to Mailchimp on the server.
JavaScript
25
star
10

youtube-url

YouTube url tools
JavaScript
21
star
11

sshout

Command line utility for running remote commands over ssh
JavaScript
21
star
12

story-json-to-video

Compile story-json documents into videos
JavaScript
19
star
13

log-rotation-stream

a writable stream that rotates into dated streams behind the scenes, so you can always append.
JavaScript
19
star
14

s3-diff

Get the diffs between a local file and a s3-folder.
JavaScript
18
star
15

cloneall

Clone/update all repositories for multiple organisations
JavaScript
17
star
16

idle-timer

Checks if a user is idle for a configurable amount of time and fires a callback
JavaScript
16
star
17

stream-read

Read from a stream, callback style.
JavaScript
14
star
18

condor

Track what a user does on a site in csv-format
JavaScript
13
star
19

stream-volume

Measure the total volume of data a stream emits
JavaScript
13
star
20

article-json-to-amp

Render article json format in the AMP format
JavaScript
13
star
21

json-extended

parse/stringify JSON, but with support for Buffers, Dates & RegExps
JavaScript
12
star
22

stream-cat

Concatenate streams, the simple way
JavaScript
12
star
23

article-json-to-fbia

Render article JSON in the Facebook Instant Articles format
JavaScript
12
star
24

article-json-to-apple-news

Render article JSON in the Apple News format
JavaScript
11
star
25

orderable

Readable stream to which you can push chunks at an index
JavaScript
11
star
26

megafunnel

artisanal big data analytics
JavaScript
11
star
27

time-bucket-reduce

reduce time series events into buckets: seconds, minutes, hours, date, month, full-year
JavaScript
11
star
28

revisions

Add multiple revisions of a document to a db
JavaScript
11
star
29

seamless-immutable-diff

JavaScript
9
star
30

csv-line

Create a line of escaped csv from an array
JavaScript
9
star
31

load-to-redshift-public

Scala
8
star
32

log-range-query

stream from ordered, dated files such as those created by log-rotation-stream
JavaScript
8
star
33

ldbcmp

compare two large leveldb instances
C++
8
star
34

immutable-array-methods

JavaScript
7
star
35

levelgraph-query

JavaScript
7
star
36

deku-pure-grid

CSS
7
star
37

funnel-stream

funnel many input streams into one stream
JavaScript
7
star
38

github-repo-from-config

Parse the github repo from `.git/config`
JavaScript
7
star
39

github-compare

Compare git branches in the GitHub UI.
JavaScript
6
star
40

facebook-scrape

Tell facebook to scrape an object
JavaScript
6
star
41

release-packager

Packages node apps.
JavaScript
6
star
42

megafunnel-view

generate views on top of megafunnel in realtime
JavaScript
6
star
43

level-time

LevelUP operations time logger
JavaScript
6
star
44

http-error-response

Standard http error handler that follows sane conventions
JavaScript
6
star
45

walk-apple-news-format

JavaScript
6
star
46

html-validate-unbalanced

Spot unbalanced tag pairs
JavaScript
6
star
47

level-log

Log all leveldb operations
JavaScript
6
star
48

image-cropper

Browserify widget to crop an image
JavaScript
6
star
49

hyperlevel-backup-to-s3

Backup level-hyper db to Amazon S3
JavaScript
6
star
50

naytev-purge

Purge the open graph-cache in naytev
JavaScript
5
star
51

aws-instances

Get a list of aws instances and their meta data.
JavaScript
5
star
52

rpc-http

simple rpc over http
JavaScript
5
star
53

inviewport

Check if a dom element is fully contained in the viewport
JavaScript
5
star
54

get-saucelabs-browsers

Given browser names & versions, get webdriver compatible config from saucelabs available browsers.
JavaScript
5
star
55

graphql-url

JavaScript
5
star
56

execute-scripts

JavaScript
5
star
57

redshift-sql

A module to get data in and out of AWS Redshift Cluster
JavaScript
5
star
58

lazy-embeds

Load embeds/iframes lazily, only when they're in the viewport
JavaScript
5
star
59

co-wd

co-compatible interface to the wd.js library (webdriver)
JavaScript
5
star
60

stream-cache-redis

Cache a stream in redis
JavaScript
5
star
61

contenteditable-placeholder

Add a placeholder to contenteditable-elements (using CSS)
JavaScript
5
star
62

keen-debug-chrome-extension

JavaScript
4
star
63

immutable-object-methods

Update plan javascript object, immutable style.
JavaScript
4
star
64

embedly-url

JavaScript
4
star
65

embeds

JavaScript
4
star
66

deku-youtube-subscribe-button

YouTube subscribe button deku component
JavaScript
4
star
67

article-json-html-render

JavaScript
4
star
68

co-webdriver-runner

Write browser tests using webdriver/selenium/chromedriver using generators/tap.
JavaScript
4
star
69

query-dom

HTML
4
star
70

install-timestamp

Post-install script that generates unique installation timestamp easily accessible by application
JavaScript
4
star
71

deku-lazy-load

JavaScript
3
star
72

proptypes-to-flow

JavaScript
3
star
73

authorized_keys-from-github

Generate a authorized_keys file based on github usernames
JavaScript
3
star
74

deku-news-ticker

JavaScript
3
star
75

level-tbr

JavaScript
3
star
76

format-call

Format a function call nicely
JavaScript
3
star
77

significant-stream

Create an object stream where insignificant changes are omitted
JavaScript
3
star
78

redirect-to-https

redirect http to https (based on the x-forwarded-proto header)
JavaScript
3
star
79

miclint

The linter we use at Mic. Based upon the Airbnb style guide
JavaScript
3
star
80

split-test-result

JavaScript
3
star
81

log-archive-s3

archive log files into s3
JavaScript
2
star
82

date-parse

Parse date value into valid Date() object or null
JavaScript
2
star
83

article-json-to-contenteditable

JavaScript
2
star
84

twitter-emojis

JavaScript
2
star
85

aws-upgrade

Upgrade aws instances.
JavaScript
2
star
86

shankwrap

shonkwrap with some extra features
JavaScript
2
star
87

max-len

JavaScript
2
star
88

load-fb-sdk

JavaScript
2
star
89

article-json-immutable-methods

JavaScript
2
star
90

hypermixer

Aggregate http based JSON api services
JavaScript
2
star
91

page-visibility

Discover if a page is visible or not.
JavaScript
2
star
92

deku-timeago

JavaScript
2
star
93

deku-element-to-jsx-string

JavaScript
2
star
94

ad-tech-project

A take home test for ad-tech engineers
JavaScript
2
star
95

deku-page-layout

Render page layout using Deku
JavaScript
2
star
96

dedupe-objects

JavaScript
2
star
97

array-merge-equal

Merge second array into the first array and overwrite equal elements
JavaScript
2
star
98

mobx-deku-boilerplate

Small project to quickly start with deku, MobX, JSX, ES6, Babel
JavaScript
2
star
99

deku-tab-control

Tab control deku component
JavaScript
2
star
100

time-period

JavaScript
2
star