spr

shaka-player-react

A simple React component wrapper for shaka-player

Showing:

Popularity

Downloads/wk

920

GitHub Stars

56

Maintenance

Last Commit

10mos ago

Contributors

1

Package

Dependencies

2

Size (min+gzip)

125.4KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Shaka Player React

A React component for Shaka Player, an open-source JavaScript library for adaptive media. It is highly recommended to check the official shaka documentation first.

Installation

npm install shaka-player-react --save

yarn add shaka-player-react

Usage

As seen in the example below, the CSS bundled with shaka-player has been imported separately. This is because shaka-player-react does not require any CSS internally, which keeps you in full control of the styling as if you were not using this React wrapper.

import React from 'react';
import ShakaPlayer from 'shaka-player-react';
import 'shaka-player/dist/controls.css';

function App() {
  return <ShakaPlayer autoPlay src="https://streams.com/example.mpd" />;
}

The following ShakaPlayer properties are supported:

PropertyDescriptionType
srcThe MPEG-DASH, or HLS media asset. Is provided to shaka.Player.load on mount or change.String
autoPlayWhether the asset should autoplay or not, directly bound to the HTMLVideoElement element.Boolean
widthWidth of the player.Number
heightHeight of the player.Number
playbackRateSets the speed of the audio/video playback.Number
mutedSets whether the video is muted or notBoolean
loopSets whether teh audio/video should start over again when finishedBoolean
volumeSets the volume of the audio/videoNumber
classNameAdds a class to the root element, which is a div.String

Access shaka's player object.

The following is a more advanced setup to illustrate how you can integrate shaka's features in React.

import React, { useRef, useEffect } from 'react';
import ShakaPlayer from 'shaka-player-react';

function App() {
  const controllerRef = useRef(null);

  useEffect(() => {
    const {
      /** @type {shaka.Player} */ player,
      /** @type {shaka.ui.Overlay} */ ui,
      /** @type {HTMLVideoElement} */ videoElement
    } = controllerRef.current;

    async function loadAsset() {
      // Load an asset.
      await player.load('https://streams.com/example.mpd');

      // Trigger play.
      videoElement.play();
    }

    loadAsset();
  }, []);

  return <ShakaPlayer ref={controllerRef} />;
}

Or check the example in this repository.

Integrate with

Next.js

import React from 'react';
import dynamic from 'next/dynamic';

const ShakaPlayer = dynamic(() => import('shaka-player-react'), { ssr: false });

export default function Index() {
  return (
    <div>
      <ShakaPlayer autoPlay src="https://streams.com/example.mpd" />
    </div>
  );
}

When using next/dynamic with { srr: false }, we'll make sure the component is not interpreted by Next.js SSR. As of today, pre-rendering shaka-player's UI is technically not possible due to the fact that it is not written in React (but in plain Javascript). Although, shaka-player heavily relies on browser API's and serves no real purpose on a backend anyways.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial