react-player

repository·master·Indexed 27 days ago

https://github.com/cookpete/react-player

A React component for playing a variety of URLs, including file paths, HLS, DASH, YouTube, Vimeo, Wistia, and Mux. It provides a unified interface to abstract the complexity of different media providers, supporting features like light mode thumbnails, responsive layouts, and custom player controls via media-chrome. Version 3.4.0 requires React 16.6 or later.

Tokens
4.9K
Snippets
13
Records
29
Agent score
94%

What's inside react-player

  1. Use Multiple Sources and Tracks

    master

    Since v3, ReactPlayer supports multiple <source> and <track> elements inside the component, similar to the native HTML5 <video> or <audio> elements.

    <ReactPlayer controls>
      <source src="foo.webm" type="video/webm">
      <source src="foo.ogg" type="video/ogg">
      <track kind="subtitles" src="subs/subtitles.en.vtt" srclang="en" default>
      <track kind="subtitles" src="subs/subtitles.ja.vtt" srclang="ja">
      <track kind="subtitles" src="subs/subtitles.de.vtt" srclang="de">
    </ReactPlayer>
  2. Migrate to react-player v3.0: Player Props

    master

    When upgrading to v3.0, several player props have been renamed to align with the HTMLMediaElement interface.

    • Rename url to src
    • Rename playsinline to playsInline
    • progressInterval is now deprecated
    • stopOnUnmount is now deprecated
    • The wrapper prop is undefined by default; set it to div if you require a wrapper element.
  3. Migrate to react-player v2.0: Lazy Loading

    master

    As of v2.2, you can use react-player/lazy to lazy load the appropriate player for a given url. This reduces your main bundle size by adding reactPlayer chunks to your output.

    Note: This requires React 16.6 or later due to the use of lazy and Suspense.

    // Before
    import ReactPlayer from 'react-player'
    
    // After
    import ReactPlayer from 'react-player/lazy'
  4. Migrate to react-player v3.0: Requirements and Limitations

    master
    • React Version: Because v3.0 uses lazy and Suspense, React 16.6 or later is required.
    • Unsupported Providers: As of v3.0, the following providers are not yet supported due to the new architecture:
      • Dailymotion
      • SoundCloud
      • Streamable
      • Twitch
      • Facebook
      • Mixcloud
      • Kaltura
  5. Migrate to react-player v2.0: Single Player Imports

    master

    As of v2.2, the location for importing single players has changed. Single players were not available in v2.0 or v2.1.

    // Before
    import ReactPlayer from 'react-player/lib/players/YouTube'
    
    // After
    import ReactPlayer from 'react-player/youtube'
  6. Use Light Mode for Video Thumbnails

    master

    The light prop renders a video thumbnail and only loads the full player upon user interaction.

    • Set light={true} to use Noembed to fetch a thumbnail.
    • Pass an image URL to light to override the preview image: <ReactPlayer light='https://example.com/thumb.png' />.
    • Pass a component to light to use a custom preview: <ReactPlayer light={<MyCustomComponent />} />.

    Target CSS classes react-player__preview, react-player__shadow, and react-player__play-icon to style the preview.

  7. Implement Custom Player Controls with Media Chrome

    master

    To build custom UI controls, you can use ReactPlayer in conjunction with media-chrome. Set controls={false} on ReactPlayer and use the slot="media" attribute.

    import ReactPlayer from "react-player";
    import {
      MediaController,
      MediaControlBar,
      MediaTimeRange,
      MediaTimeDisplay,
      MediaVolumeRange,
      MediaPlaybackRateButton,
      MediaPlayButton,
      MediaSeekBackwardButton,
      MediaSeekForwardButton,
      MediaMuteButton,
      MediaFullscreenButton,
    } from "media-chrome/react";
    
    export default function Player() {
      return (
        <MediaController
          style={{
            width: "100%",
            aspectRatio: "16/9",
          }}
        >
          <ReactPlayer
            slot="media"
            src="https://stream.mux.com/maVbJv2GSYNRgS02kPXOOGdJMWGU1mkA019ZUjYE7VU7k"
            controls={false}
            style={{
              width: "100%",
              height: "100%",
              "--controls": "none",
            }}
          ></ReactPlayer>
          <MediaControlBar>
            <MediaPlayButton />
            <MediaSeekBackwardButton seekOffset={10} />
            <MediaSeekForwardButton seekOffset={10} />
            <MediaTimeRange />
            <MediaTimeDisplay showDuration />
            <MediaMuteButton />
            <MediaVolumeRange />
            <MediaPlaybackRateButton />
            <MediaFullscreenButton />
          </MediaControlBar>
        </MediaController>
      );
    }
  8. Migrate to react-player v3.0: Player Callback Props

    master

    In v3.0, callback props have been renamed to match native HTMLMediaElement event naming:

    • onProgress $\rightarrow$ onTimeUpdate and onProgress
    • onDuration $\rightarrow$ onDurationChange
    • onPlaybackRateChange $\rightarrow$ onRateChange
    • onSeek $\rightarrow$ onSeeking and onSeeked
    • onBuffer $\rightarrow$ onWaiting
    • onBufferEnd $\rightarrow$ onPlaying
    • onEnablePIP $\rightarrow$ onEnterPictureInPicture
    • onDisablePIP $\rightarrow$ onLeavePictureInPicture
  9. Migrate to react-player v2.0: Use onReady to access player instance

    master

    In v2.0, the onReady callback is invoked with the ReactPlayer instance. This allows you to store the instance and call methods (like getInternalPlayer) immediately without worrying about whether the player is ready.

    // After v2.0
    class Player extends Component {
      handleReady = player => {
        this.player = player            // Store a player that is ready for methods
        this.player.getInternalPlayer() // Internal player now ready
      }
      render () {
        return (
          <ReactPlayer onReady={this.handleReady} />
        )
      }
    }
  10. Migrate to react-player v2.0: Configure players via the config prop

    master

    In v2.0, deprecated player-specific config props (like youtubeConfig) have been removed. All player configurations must now be passed through the single config prop.

    If you are only using one type of URL, you can place player-specific options directly inside config without a provider key.

    // Before
    <ReactPlayer 
      youtubeConfig={{ playerVars: { showinfo: 1 } }} 
    />
    
    // After (using provider key)
    <ReactPlayer 
      config={{ youtube: { playerVars: { showinfo: 1 } }}} 
    />
    
    // After (direct config for single-provider use cases)
    <ReactPlayer 
      config={{ playerVars: { showinfo: 1 } }} 
    />