• Stars
    star
    470
  • Rank 93,399 (Top 2 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

React audio and video player.

React Media Player

Components and decorators to help build video & audio players in React. Supports HTML5, Youtube, and Vimeo media types.

Install

npm install react-media-player --save

<script src="https://unpkg.com/react-media-player/dist/react-media-player.js"></script>
(UMD library exposed as `ReactMediaPlayer`)

Media component

A special wrapper component that collects information from the Player component and passes down the proper props to withMediaProps decorator.

Player component

This is another special component that renders your player and communicates with the Media wrapper.

src: PropTypes.string.isRequired

This is the source of the media you want to play. Currently supporting Youtube, Vimeo, and any HTML5 compatible video or audio.

vendor: PropTypes.oneOf(['youtube', 'vimeo', 'audio', 'video'])

Explicitly choose which internal component to render for the player. If nothing is set, the library does its best to determine what player to render based on the source passed in.

autoPlay: PropTypes.bool

Autoplay media when the component is mounted or src changes.

loop: PropTypes.bool

Loop the current src indefinitely.

useAudioObject: PropTypes.bool

When playing HTML5 audio, it will construct audio using the Audio class instead of rendering an element to the page.

connectSource: PropTypes.func(source, audioContext)

A chance to connect a series of AudioNode[s] when using the audio vendor. Must return a new audio node that will be connected to audioContext.destination internally.

onPlay: PropTypes.func

Callback when media starts playing.

onPause: PropTypes.func

Callback when media has been paused.

onError:PropTypes.func

Callback when an error occurs.

onDuration: PropTypes.func

Callback when the duration of the media has been calculated.

onProgress: PropTypes.func

Callback when media starts downloading.

onTimeUpdate: PropTypes.func

Callback when media time has changed.

onMute: PropTypes.func

Callback when the player has been muted.

onVolumeChange: PropTypes.func

Callback when the player volume has changed.

import React, { Component } from 'react'
import { Media, Player, controls } from 'react-media-player'
const { PlayPause, MuteUnmute } = controls

class MediaPlayer extends Component {
  render() {
    return (
      <Media>
        <div className="media">
          <div className="media-player">
            <Player src="http://www.youtube.com/embed/h3YVKTxTOgU" />
          </div>
          <div className="media-controls">
            <PlayPause />
            <MuteUnmute />
          </div>
        </div>
      </Media>
    )
  }
}

withMediaProps decorator props exposed under this.props.media

Passes down helpful state information and methods to build custom media player controls. Please note that children must be wrapped in the Media component.

currentTime: PropTypes.number

progress: PropTypes.number

duration: PropTypes.number

volume: PropTypes.number

isLoading: PropTypes.bool

isPlaying: PropTypes.bool

isMuted: PropTypes.bool

isFullscreen: PropTypes.bool

play: PropTypes.func

pause: PropTypes.func

playPause: PropTypes.func

stop: PropTypes.func

seekTo: PropTypes.func

mute: PropTypes.func

muteUnmute: PropTypes.func

setVolume: PropTypes.func

fullscreen: PropTypes.func

import React, { Component } from 'react'
import { withMediaProps } from 'react-media-player'

class CustomPlayPause extends Component {
  shouldComponentUpdate({ media }) {
    return this.props.media.isPlaying !== media.isPlaying
  }

  _handlePlayPause = () => {
    this.props.media.playPause()
  }

  render() {
    const { className, style, media } = this.props
    return (
      <button
        type="button"
        className={className}
        style={style}
        onClick={this._handlePlayPause}
      >
        {media.isPlaying ? 'Pause' : 'Play'}
      </button>
    )
  }
}

export default withMediaProps(CustomPlayPause)
import React from 'react'
import CustomPlayPause from './CustomPlayPause'

function App() {
  return (
    <Media>
      <CustomPlayPause />
      <Player src="https://youtu.be/VOyYwzkQB98" />
    </Media>
  )
}

export default App

utils.keyboardControls

A special function that will provide keyboard support to the media player.

import React, { Component } from 'react'
import { Media, Player, controls, utils } from 'react-media-player'
const {
  PlayPause,
  CurrentTime,
  Progress,
  SeekBar,
  Duration,
  MuteUnmute,
  Volume,
  Fullscreen,
} = controls
const { keyboardControls } = utils

class MediaPlayer extends Component {
  render() {
    const { Player, keyboardControls } = this.props
    return (
      <Media>
        {mediaProps => (
          <div
            className="media"
            onKeyDown={keyboardControls.bind(null, mediaProps)}
          >
            <Player src="against-them-all.mp3" className="media-player" />
            <div className="media-controls">
              <PlayPause />
              <CurrentTime />
              <Progress />
              <SeekBar />
              <Duration />
              <MuteUnmute />
              <Volume />
              <Fullscreen />
            </div>
          </div>
        )}
      </Media>
    )
  }
}

utils.formatTime

A helper function to format time.

import React, { Component } from 'react'
import { withMediaProps, utils } from 'react-media-player'
const { formatTime } = utils

class CurrentTime extends Component {
  shouldComponentUpdate({ media }) {
    return this.props.media.currentTime !== media.currentTime
  }

  render() {
    const { className, style, media } = this.props
    return (
      <time className={className} style={style}>
        {formatTime(media.currentTime)}
      </time>
    )
  }
}

export default withMediaProps(CurrentTime)

Running Locally

clone repo

git clone [email protected]:souporserious/react-media-player.git

move into folder

cd ~/react-media-player

install dependencies

npm install

run dev mode

npm run dev

open your browser and visit: http://localhost:8080/

More Repositories

1

react-measure

πŸ“ Compute measurements of a React component.
JavaScript
1,940
star
2

react-motion-ui-pack

Wrapper component around React Motion for easier UI transitions
JavaScript
625
star
3

renoun

Documentation that matches the quality of your product.
TypeScript
416
star
4

restyle

The simplest way to add CSS styles to your React components.
TypeScript
304
star
5

react-view-pager

Slider/Carousel powered by React Motion.
JavaScript
293
star
6

jsxui

The design compiler for JSX.
TypeScript
175
star
7

unicon

πŸ¦„ Wrangle SVGs from your favorite design tool.
JavaScript
169
star
8

next-remote-refresh

Enable Fast Refresh for remote data in Next.js
JavaScript
151
star
9

react-aria

Utility components to help compose React ARIA components
JavaScript
144
star
10

react-fluid-container

Graceful dynamic/variable height animation
JavaScript
133
star
11

tsxmod

Refactor code, migrate to new APIs, and better understand your codebase.
TypeScript
111
star
12

reforest

🌲 Collect ordered React render data seamlessly across the server and client.
TypeScript
79
star
13

use-item-list

A primitive React hook used to coordinate indexed collections effortlessly.
JavaScript
74
star
14

lock-scrollbars

Lock all scrollbars to prevent scrolling the page.
TypeScript
73
star
15

sketch-to-svg-json-loader

Sketch to SVG JSON loader module for webpack
JavaScript
63
star
16

documenting-systems-in-nextjs

Case study of how to build a documentation system in Next.js using TS Morph.
TypeScript
52
star
17

mdx-to-md

Convert MDX to Markdown.
TypeScript
51
star
18

next-monaco

TypeScript
45
star
19

react-paper-ripple

Paper ripple animations inspired by Google Material Design
JavaScript
31
star
20

outy

A simple way to listen for events outside of elements in 390 bytes
JavaScript
31
star
21

react-matches

Simple helper components to make responsive design easier in React.
JavaScript
29
star
22

next-dx

Enhance Next.js development with utilities for local and remote content.
TypeScript
21
star
23

react-component-query

Component queries in React
JavaScript
21
star
24

animated-ui

Components for UI animations powered by Animated
JavaScript
21
star
25

build-your-own-code-playground

A simple React code playground built in Next.js
TypeScript
20
star
26

get-node-dimensions

Get accurate element dimensions, even if it's hidden!
JavaScript
20
star
27

selectly

Build composeable select menus for React.
JavaScript
19
star
28

react-midnight

Handle dates, times, and calendars with React.
JavaScript
17
star
29

docgraph

Generate rich documentation data for TypeScript and MDX codebases.
TypeScript
17
star
30

velocitytransitiongroup

React addon component to easily use Velocity animations in React.
JavaScript
17
star
31

use-roving-index

Manage an active index that needs to be contained or wrapped.
TypeScript
16
star
32

golly

Powerful flexbox grid system built on top of glamor.
JavaScript
12
star
33

tonality

Some small functions to help build and adjust color tone palettes.
JavaScript
11
star
34

use-scroll-sync

Sync scroll positions between multiple elements.
TypeScript
10
star
35

use-ref-list

Easily manage an ordered list of refs in React.
JavaScript
8
star
36

react-motion-sortable

Sortable components powered by React Motion.
JavaScript
8
star
37

react-travel

JavaScript
8
star
38

bundling-typescript-with-esbuild-for-npm

Example of bundling TypeScript using Esbuild.
JavaScript
7
star
39

create-styled-element

Simple wrapper around glamor to create styled elements in React.
JavaScript
6
star
40

monaco-textmate-syntax-highlighting

Monaco TextMate Syntax Highlighting in Next.js
TypeScript
6
star
41

nextjs-remote-mdx-fast-refresh

Workaround to get Fast Refresh working for remote MDX content in a local directory.
JavaScript
5
star
42

next-monaco-vscode-api

Monaco Editor x Next.js
TypeScript
5
star
43

react-editable

Build flexible WYSIWYG editors with ease
JavaScript
4
star
44

react-aria-ui

Collection of ARIA compliant React components.
JavaScript
4
star
45

react-rails-theme-provider

Demo of setting up ThemeProvider with react-rails
Ruby
4
star
46

app-router-form

App Router testing.
TypeScript
4
star
47

react-drop

Drop content anywhere on the page
JavaScript
4
star
48

generate-typescript-docs-using-ts-morph

Generate docs from TypeScript using TS Morph.
TypeScript
4
star
49

get-prefix

Get browser style prefixes for Javascript.
JavaScript
4
star
50

animation-bus

Define different animation stops relative to another value.
JavaScript
4
star
51

better-library-dx-using-jsdoc-links

Example of using Babel to enrich JSDoc links with helpful links.
JavaScript
3
star
52

react-cli-example

Example showing how to bundle a CLI tool using react ink and tsup.
JavaScript
3
star
53

scroll-media

Control videos or audio based on scroll position.
JavaScript
3
star
54

preact-signals-nextjs

TypeScript
3
star
55

managing-design-systems-with-typescript

An example of how to manage Design Systems with TypeScript.
TypeScript
3
star
56

site

My personal website πŸ’€
JavaScript
3
star
57

babel-plugin-extract-export

Extract ESM exports and their dependencies using Babel.
TypeScript
3
star
58

contours

An example of generating contours from a 2D array using Rust.
TypeScript
2
star
59

style-bits

JavaScript
2
star
60

reanime

JavaScript
2
star
61

esbuild-figma-plugin

Example showing how to compile Figma plugins using Esbuild.
TypeScript
2
star
62

server-components-css

TypeScript
2
star
63

mdx-bundler-and-styled-components

TypeScript
2
star
64

origins-boilerplate

A starting place for the web. Will add config settings soon.
JavaScript
2
star
65

use-frame

Sync animation frames in React.
TypeScript
2
star
66

docs-example

TypeScript
1
star
67

extract-exports-using-ts-morph

An example of how to extract an ESM export using TS Morph.
TypeScript
1
star
68

args-flags

A simple command line argument parser for Node.
JavaScript
1
star
69

calendar-fns

Small utility functions for building calendars in JavaScript.
TypeScript
1
star
70

data-slider

A simple slider that allows data attributes to control it.
JavaScript
1
star
71

vscode-ast-explorer

TypeScript
1
star
72

next-mdx-table-bug

JavaScript
1
star
73

mdx-use-client

JavaScript
1
star