@mux/mux-player-react
TypeScript icon, indicating that this package has built-in type declarations

0.1.0-canary.66-bbf1e00 • Public • Published

<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>

Hiding controls

By default, Mux Player will show all the controls associated with the current player size and stream type.

To hide certain controls, use CSS variables like this:
--play-button will control the display of the play button. Set it to none to hide it completely.

mux-player {
  --play-button: none;
}

CSS vars can also be passed inline

<MuxPlayer style={{ '--play-button': 'none' }}></MuxPlayer>

Controls sections

It's possible to target specific sections of the player by prefixing the CSS vars.
The following sections are available:

  • top the top control bar that shows on the small player size
  • center the center controls that show the seek forward/backward button and play button
  • bottom the bottom control bar
<MuxPlayer style={{ '--center-controls': 'none', '--top-captions-button': 'none' }}></MuxPlayer>

Available CSS variables

The below CSS selector shows all available CSS vars for hiding, each one can be prefixed with a section.

mux-player {
  /* Hide all controls at once */
  --controls: none;

  /* Target all sections by excluding the section prefix */
  --play-button: none;
  --seek-live-button: none;
  --seek-backward-button: none;
  --seek-forward-button: none;
  --mute-button: none;
  --captions-button: none;
  --airplay-button: none;
  --pip-button: none;
  --fullscreen-button: none;
  --cast-button: none;
  --playback-rate-button: none;
  --volume-range: none;
  --time-range: none;
  --time-display: none;
  --duration-display: none;

  /* Target a specific section by prefixing the CSS var with (top|center|bottom) */
  --center-controls: none;
  --bottom-play-button: none;
}

CSS Parts

Mux Player uses a shadow DOM to encapsulate its styles and behaviors. As a result, it's not possible to target its internals with the usual CSS selectors. Instead, some components expose parts that can be targeted with the CSS part selector , or ::part().

<!-- Global style in HTML or added using preferred React styles convention/library -->
<style>
  mux-player::part(center play button) {
    display: none;
  }
</style>
<MuxPlayer playback-id="DS00Spx1CV902MCtPj5WknGlR102V5HFkDe"></MuxPlayer>

Supported parts: seek-live, layer, media-layer, poster-layer, vertical-layer, centered-layer, gesture-layer, top, center, bottom, play, button, seek-backward, seek-forward, mute, captions, airplay, pip, cast, fullscreen, playbackrate, volume, range, time, display

CSS parts allow you to style each part individually with a selector like ::part(center play button) or target multiple elements if the part is assigned to multiple elements internally, usage ::part(button). Every CSS property can be declared in the selector, this makes it a very powerfull API.

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_giGLg-byJPuu_eUqu1MW9bILLly_9gq8m0cNKghUa9xTMJgFmaya4XYudy5Mt2Fu72MiS3csUP3xhKlONVnGHlMRqB-dBVOgAJrayeUquAhaNY346oFBUWVM-EcAZ9G2ARtPakfy4Wpv5BsRKEGtR81P-k7EW8g27U0FKLlrvLkUz3Z-JYu53CRcJUvjkC9sDMrZLcTA',
    thumbnail:
      'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtp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65IDm-ZEoJrOzmZctMWAQ',
  }}
/>

preferPlayback

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 preferPlayback prop to mse.

Example:

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

Props

Prop Type Description Default
playbackId string The 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
envKey string Your 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" |"live:dvr" | "ll-live:dvr" 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"
audio boolean Indicate that you want an "audio only" UI/chrome. This may be used for audio-only assets or audio+video assets. false
metadata object* An object for configuring any metadata you'd like to send to Mux Data. For more, see the Metadata section, below. N/A
tokens object* An object for configuring any tokens for your assets if you're using Signed URLs. For more, see the Tokens section, below. N/A
debug boolean Enables debug mode for the underlying playback engine (currently hls.js) and mux-embed, providing additional information in the console. false
startTime number (seconds) Specify where in the media's timeline you want playback to start. 0
thumbnailTime number (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
preferPlayback "mse" | "native" | undefined Set the preference for the playback type; mse (currently hls.js), or native playback if it is supported (e.g. in Safari). For more, see the section on preferPlayback undefined
defaultHiddenCaptions boolean Hide captions by default instead of showing them on initial load (when available) false
defaultShowRemainingTime boolean Show remaining playback time (instead of current playback time) by default false
forwardSeekOffset number (seconds) Offset applied to the forward seek button 10
backwardSeekOffset number (seconds) Offset applied to the backward seek button 10
primaryColor (Any valid CSS color style) The primary color used by the player N/A
secondaryColor (Any valid CSS color style) The secondary color used by the player N/A
currentTime number (seconds) Sets the current time of the media N/A
volume number (0-1) Sets the volume of the player from 0 to 1. Varies
muted boolean Toggles the muted state of the player. Varies
paused boolean Toggles the paused state of the player N/A
autoPlay boolean Toggles whether or not media should auto-play when initially loaded N/A
playbackRate number Applies a multiplier to the media's playback rate, either speeding it up or slowing it down. 1
playbackRates number[] The array of numbers that will be used by the playback rate button while toggling through rates. 1
loop boolean Automatically loop playback of your media when it finishes. false
nohotkeys boolean Toggles keyboard shortcut (hot keys) support when focus in inside the player. false
playsInline boolean Set to assert that media should be played inline. Useful for mobile playback cases. false
crossOrigin string Establishes various CORS policies. For more details, see MDN N/A
title string Show the title for your content. ""
poster string (URL) Assigns a poster image URL. Will use the automatically generated poster based on your playback-id by default. Derived
placeholder string (URL) The src URL or data URI for the placeholder image shown while poster loads. N/A
beaconCollectionDomain string (Domain name) Assigns a custom domain to be used for Mux Data collection. N/A
customDomain string (Domain name) Assigns a custom domain to be used for Mux Video. Will use the standard mux.com domain with your playback-id for poster, video, and thumbnail URLs by default. N/A

Callbacks

<MuxPlayer/> has a number of callbacks associated with events for media loading, playback, and the player itself. For example, a callback for 'loadstart event isonLoadStart`. See mux-player's document for detail of events.

Package Sidebar

Install

npm i @mux/mux-player-react@0.1.0-canary.66-bbf1e00

Version

0.1.0-canary.66-bbf1e00

License

MIT

Unpacked Size

112 kB

Total Files

26

Last publish

Collaborators

  • mux-npmjs