Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mux/mux-player-react

Package Overview
Dependencies
Maintainers
44
Versions
562
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mux/mux-player-react

An open source Mux player for React that Just Works™

  • 0.1.0-canary.0-556dabf
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
75K
decreased by-22.53%
Maintainers
44
Weekly downloads
 
Created
Source

<MuxPlayer/>

Downloads Version License

Introduction

<MuxPlayer/> is a Mux-flavored React video player component, built on top of our mux-player web componnent and used to play Mux Video content that Just Works.

Be sure to check out our official Mux documentation, too!

Installation

If you're using npm or yarn, install that way:

Package manager

yarn add @mux/mux-player-react

or

npm i @mux/mux-player-react

Then, import the library into your application with either import or require:

import MuxPlayer from '@mux/mux-player-react';

or

const MuxPlayer = require('@mux/mux-player-react');

Features and benefits

<MuxPlayer/> is a fully functional Video Player for the web with dirt simple integration to Mux Video and Mux Data.

<MuxPlayer/> provides a responsive UI based on player dimensions and stream type, automatic thumbnail previews and poster images, and built-in integration with Mux Data.

<MuxPlayer/> will use the optimial Hls.js settings for Mux Video so you don't have to worry about that. <MuxPlayer/> will also periodically test new versions of Hls.js and upgrade to known stable versions so you don't have to worry about upgrading to a new version of Hls.js yourself.

Usage

Under the hood, loading this library in the browser will register a custom web component for <mux-player>, but we present you with a "React-flavored" component to use it. Here's a simple example:

const MuxPlayerExample = () => {
  return (
    <div>
      <h1>Simple MuxPlayer Example</h1>
      <MuxPlayer
        style={{ height: '100%', maxWidth: '100%' }}
        playbackId="DS00Spx1CV902MCtPj5WknGlR102V5HFkDe"
        metadata={{
          video_id: 'video-id-123456',
          video_title: 'Super Interesting Video',
          viewer_user_id: 'user-id-bc-789',
        }}
        streamType="on-demand"
        autoPlay
        muted
      />
    </div>
  );
};

Metadata

Providing Mux Data Metadata allows you to take full advantage of analytics and make the data more actionable, even getting metrics on how many unique viewers your content has For a detailed discussion of the available metadata fields and what they represent, check out the Mux Data docs. A few high priority keys that you'll likely want to set are:

  • video_id: string: Your internal ID for the video.
  • video_title: string: Title of the video player (e.g.: 'Awesome Show: Pilot')
  • viewer_user_id: string: An ID representing the viewer who is watching the stream. Use this to look up video views for an individual viewer. If no value is specified, a unique ID will be generated by the SDK. Note: You should not use any value that is personally identifiable on its own (such as email address, username, etc). Instead, you should supply an anonymized viewer ID which you have stored within your own system.

Example:

<MuxPlayer
  playback-id="DS00Spx1CV902MCtPj5WknGlR102V5HFkDe"
  metadata={{
    video_id: 'video-id-123456',
    video_title: 'Super Interesting Video',
    viewer_user_id: 'user-id-bc-789',
  }}
/>

Chromecast

Enable the Google Cast button in the controlbar by dropping in the <script> tag below in the <head> of your webpage.

<script defer src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1"></script>

Tokens

Using JSON Web Tokens allows you to secure your media content from public playback, and Mux Video provides a way to do set this up for your assets. To apply these tokens in <MuxPlayer/>, you can use the tokens property. <MuxPlayer/> will automatically generate appropriate URLs for each asset for any provided tokens. The possible tokens are playback (for the video asset/playback-id), thumbnail (for the poster image), and storyboard (for the seek preview thumbnail images).

Example:

<MuxPlayer
  playback-id="g65IqSFtWdpGR100c2W8VUHrfIVWTNRen"
  tokens={{
    playback:
      'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik96VU90ek1nUWhPbkk2MDJ6SlFQbU52THR4MDBnSjJqTlBxN0tTTzAxQlozelEifQ.eyJleHAiOjE2NDY0Mzg5NjEsImF1ZCI6InYiLCJzdWIiOiJiemVVNWZSQTQ3UzAxS0R6ck9iWWlpWnZ6ajAwajVFMDBkQ1ZidDNvUnptZkYwMCJ9.hWrdcJDa8FJCfVFP19oJ-9FSEVk9eB6DTOCRrucnzsrtUoZbb1OFe7swpQ38Fp3hZNNIt7-LWjdOl90TF4ucu7mhu42qyk3_i054RtmEZyQaj5Qjm3_H4sa2jLO-0QNSnOfp1A9x-fI8M_giGL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IITCSsIeTgXcI667B2GWJ5juDIErz1h-NQsPIfB-FsUeuWx2rYOap4G3FdwEIjaGc29HPncw-mG0JLcqkMB7jtDxjBY_-YlpjFYJF_z7r-1yIJM7mF3rl8YqeWstojC8oh2Iv2VRkuTyPE31QVI6fQcet5PIRWHudUIGWcNiWM56vwZskJ6qod8UvYpha7K5rhshh0Xdhnvq3Y9b6PXl3fy6VKCZIyszlPVje0IR2bR9iHDXnGbawivUsI65IDm-ZEoJrOzmZctMWAQ',
  }}
/>

preferMse

By default <MuxPlayer/> will try to use native playback via the underlying <video/> tag whenever possible. However, it can also instead use an in-code player as long as the browser supports Media Source Extensions. This includes MSE in Mac OS Safari.

If you prefer to use the in-code MSE-based engine (currently hls.js) whenever possible, then simply set the preferMse prop.

Example:

<MuxPlayer
  playback-id="DS00Spx1CV902MCtPj5WknGlR102V5HFkDe"
  metadata={{
    video_id: 'video-id-123456',
    video_title: 'Super Interesting Video',
    viewer_user_id: 'user-id-bc-789',
  }}
  preferMse
/>

Props

PropTypeDescriptionDefault
playbackIdstringThe playback ID for your Mux Asset or Mux Live Stream. This will also be used for automatically assigning a poster image and (thumbnail previews)[https://docs.mux.com/guides/video/create-timeline-hover-previews]. For more, check out the Mux Docs.N/A
envKeystringYour Mux Data environment key. Note that this is different than your API Key. Get your env key from the "Mux Data" part of your Mux Environments Dashboard. If undefined, the environment will be inferred based on your Mux Video asset.undefined
streamType"on-demand" | "live" | "ll-live"The type of stream associated with your Mux Asset. Used to determine what UI/controls to show and what optimizations to make for playback."on-demand"
audiobooleanIndicate that you want an "audio only" UI/chrome. This may be used for audio-only assets or audio+video assets.false
metadataobject*An object for configuring any metadata you'd like to send to Mux Data. For more, see the Metadata section, below.N/A
tokensobject*An object for configuring any tokens for your assets if you're using Signed URLs. For more, see the Tokens section, below.N/A
debugbooleanEnables debug mode for the underlying playback engine (currently hls.js) and mux-embed, providing additional information in the console.false
startTimenumber (seconds)Specify where in the media's timeline you want playback to start.0
thumbnailTimenumber (seconds)Offset for the poster image you want to show before loading media. If no thumbnailTime is specified, startTime will be used by default. NOTE: This feature currently cannot be used with tokens.thumbnail.0
preferMsebooleanUse the underlying playback engine (currently hls.js), even if native playback is supported (e.g. in Safari). For more, see the section on preferMsefalse
defaultHiddenCaptionsbooleanHide captions by default instead of showing them on initial load (when available)false
forwardSeekOffsetnumber (seconds)Offset applied to the forward seek button10
backwardSeekOffsetnumber (seconds)Offset applied to the backward seek button10
primaryColor(Any valid CSS color style)The primary color used by the playerN/A
secondaryColor(Any valid CSS color style)The secondary color used by the playerN/A
tertiaryColor(Any valid CSS color style)The tertiary color used by the playerN/A
currentTimenumber (seconds)Sets the current time of the mediaN/A
volumenumber (0-1)Sets the volume of the player from 0 to 1.Varies
mutedbooleanToggles the muted state of the player.Varies
pausedbooleanToggles the paused state of the playerN/A
autoPlaybooleanToggles whether or not media should auto-play when initially loadedN/A
playbackRatenumberApplies a multiplier to the media's playback rate, either speeding it up or slowing it down.1
loopbooleanAutomatically loop playback of your media when it finishes.false
playsInlinebooleanSet to assert that media should be played inline. Useful for mobile playback cases.false
crossOriginstringEstablishes various CORS policies. For more details, see MDNN/A
posterstring (URL)Assigns a poster image URL. Will use the automatically generated poster based on your playback-id by default.Derived

Callbacks

<MuxPlayer/> has a number of callbacks associated with events for media loading, playback, and the player itself.

PropDescription
onLoadStartInvoked when the media starts loading
onLoadedMetadataInvoked when the media's metadata has loaded
onProgressDescription
onDurationChangeInvoked when the media's duration changes, generally either because live media is coming to an end or a new playback-id has been set
onVolumeChangeInvoked when the player's volume changes
onRateChangeInvoked when the player's playbackRate changes
onResizeInvoked when the player resizes
onWaitingInvoked when playback is waiting to load more media to play (rebuffering).
onPlayInvoked when playback begins
onPlayingInvoked when playback is ready after e.g. a pause or temporary rebuffering
onPauseInvoked when playback is paused
onTimeUpdateInvoked when the media's currentTime changes, either from playback or seeking
onSeekingInvoked when an attempt to seek forward or backward in the media begins
onSeekedInvoked when an attempt to seek forward or backward in the media finishes
onEndedInvoked when media playback reaches the end of the media
onErrorInvoked when an error occurs in playback or in the player

FAQs

Package last updated on 28 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc