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

byteark-player-react

Package Overview
Dependencies
Maintainers
0
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

byteark-player-react

ByteArk Player Container for React

  • 3.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
213
decreased by-52.98%
Maintainers
0
Weekly downloads
 
Created
Source

ByteArk Player Container for React

NPM JavaScript Style Guide

Demo

You can try on the demo page.

Features

  • Remote player updates. No need to update your code for minor improvements.
  • Using placeholder to maintain page's layout before the player ready.
  • Controls basic behaviours via props.
  • Custom advance behaviours via callbacks to access ByteArk Player/VideoJS instance directly.
  • Supported TypeScript

Installation

This library is distributed via NPM. You may install from NPM or Yarn.

# For NPM
npm install --save byteark-player-react
# For Yarn
yarn add byteark-player-react
# For pnpm
pnpm add byteark-player-react

Usage

Include ByteArkPlayerContainer component into your component.

import React from 'react'
import { render } from 'react-dom'
import { ByteArkPlayerContainer } from 'byteark-player-react'
import type { ByteArkPlayerContainerProps } from 'byteark-player-react'

const App = () => {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    aspectRatio: '16:9',
    poster: 'https://stream-image.byteark.com/image/video-cover-480p/7/H/7H6hEZrLH.png',
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC',
        poster: 'https://stream-image.byteark.com/image/video-cover-480p/7/H/7H6hEZrLH.png'
      }
    ]
  }
  return <ByteArkPlayerContainer {...playerOptions} />
}

render(<App />, document.getElementById('root'))

If the video will be displayed on the fixed-size container, you may want to use fill mode instead.

import React from 'react'
import { render } from 'react-dom'
import { ByteArkPlayerContainer } from 'byteark-player-react'
import type { ByteArkPlayerContainerProps } from 'byteark-player-react'

const App = () => {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fill: true,
    poster: 'https://stream-image.byteark.com/image/video-cover-480p/7/H/7H6hEZrLH.png',
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC',
        poster: 'https://stream-image.byteark.com/image/video-cover-480p/7/H/7H6hEZrLH.png'
      }
    ]
  }

  return <ByteArkPlayerContainer {...playerOptions} />
}

render(<App />, document.getElementById('root'))

Basic Props

Following properties are the properties that can be updated to the player, without re-creating the player instance. Additional properties will be passed to player.

NameTypeDefaultDescription
autoplayBoolean/StringtrueAutoplay the video after player is created. (true/false/'muted'/'play'/'any')
aspectRatioString-Use with fluid layout mode, to inform expected video's aspect ratio (16:9)
controlsBooleantrueShow/hide the controls bar.
fillBoolean-Use fill layout mode.
fluidBoolean-Use fluid layout mode.
loopBoolean-Restart the video playback after plays to the end.
mutedBoolean-Play the video without sounds.
playerSlugIdString-SlugId of player created via api player server service
playerVersionString1.0Version of the player to use.
playbackRateNumber1.0Playback speed. 1.0 means original speed.
playsinlineBooleantrueShould be true so custom controls available on all platforms, including iOS.
posterString-Image to be show before the video is playing.
preloadString-Preload the video before play. ('none'/'metadata'/'auto')
responsiveBoolean-Auto show/hide controls depending on the screen size.
seekButtonsBooleanfalseShow 10 seconds seek buttons and allow double-tap to seek on mobile.
sourcesArray-Array of video source object to be played.
volumeNumber-Video's volume, between 0 to 1.

You can also use other props not listed here, but appear as VideoJS's options. However, changing props will not effective after the player is created.

Source Object

The source object has 2 required fields, and more optional field:

NameTypeDescription
srcStringURL to the video.
typeStringVideo content type.
titleStringVideo title to display on the player.
videoIdStringVideo's ID, usually used on Analytics.
posterStringPoster image URL for the video.

To provide multiple version of sources, you can use array of source objects.

Callback Props

We also provide some callback properties, so you can inject some behaviours directly to the ByteArk Player, and also, to the VideoJS's instance.

NameTypeCallback ParametersDescription
onPlayerLoadedFunction-Callback function to be called when loaded player's resources.
onPlayerLoadingErrorFunction({error: ByteArkPlayerContainerError, originalError: ByteArkPlayerError)Callback function to be called when there're an error about loading player.
onPlayerSetupFunction-Callback function to be called when player is setup.
onPlayerSetupErrorFunction({error: ByteArkPlayerContainerError, originalError: ByteArkPlayerError)Callback function to be called when there're an error when setup player.
onReadyFunction(player: ByteArkPlayer)Callback function to be called when a player instance is ready.
onPlayerCreatedFunction(player: ByteArkPlayer)Callback function to be called when a player instance is created.

Advanced Props

We also provide some ways to custom the appearance of the video placeholder, and some advance behaviours.

NameTypeDescription
createPlaceholderFunctionFunctionCustom video placeholder. This function should return a React component.
createPlayerFunctionFunctionCustom video instance. This function should return a VideoJS's player instance.
lazyloadBooleanThe player loads its asset files once it got rendered on the webpage. By passing this prop, the player then loads its asset files once the user clicked on the player instead.
playerEndpointStringEndpoint to the video player (without version part).
playerJsFileNameStringFile name of player's JS.
playerCssFileNameStringFile name of player's CSS.
import { ByteArkPlayerContainer } from 'byteark-player-react'

const App = () => {
  // An Advanced Props Example
  return
    <ByteArkPlayerContainer
      {...playerOptions}
      playerEndpoint='my-custom-endpoint'
      lazyload />
}

Advanced Usages

Controlling Players

You may access the player instance from onReady callback parameter.

// This following example allows user to play/pause the video playback
// from custom buttons outside.

import React from 'react'
import { render } from 'react-dom'
import { ByteArkPlayerContainer } from 'byteark-player-react'
import type { ByteArkPlayerContainerProps, ByteArkPlayer } from 'byteark-player-react'

const App = () => {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC'
      }
    ]
  }

  let playerInstance = null
  const onReady = (newPlayerInstance: ByteArkPlayer) => {
    playerInstance = newPlayerInstance
  }

  return <div>
    <ByteArkPlayerContainer {...playerOptions} onReady={onReady} />
    <button onClick={() => playerInstance?.play()}>Play</button>
    <button onClick={() => playerInstance?.pause()}>Pause</button>
  </div>
}

render(<App />, document.getElementById('root'))

Using VideoJS Plugins

import React from 'react'
import { render } from 'react-dom'
import { ByteArkPlayerContainer } from 'byteark-player-react'
import type { ByteArkPlayerContainerProps, ByteArkPlayer } from 'byteark-player-react'

const App = () => {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC'
      }
    ]
  }

  const onReady = (newPlayerInstance: ByteArkPlayer) => {
    // The player is ready! Initialize plugins here.
    newPlayerInstance.somePlugin()
  }

  return <ByteArkPlayerContainer {...playerOptions} onReady={onReady} />
}

render(<App />, document.getElementById('root'))

Request Media/Encryption with credentials

import React from 'react'
import { render } from 'react-dom'
import { ByteArkPlayerContainer } from 'byteark-player-react'
import type { ByteArkPlayerContainerProps, ByteArkPlayer } from 'byteark-player-react'

const App = () => {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC'
      }
    ],
    html5: {
      hlsjs: {
        xhrSetup: function(xhr: XMLHttpRequest, url: string) {
          xhr.withCredentials = true
        }
      }
    }
  }

  const onReady = (newPlayerInstance: ByteArkPlayer) => {
    // The player is ready! Initialize plugins here.
  }

  return <ByteArkPlayerContainer {...playerOptions} onReady={onReady} />
}

Use with Next.js

ByteArkPlayer required HTML DOM element and browser-specific APIs to use with Next.js additional steps

  1. Create byteark-player.tsx file and make it to be client component by adding "use client" directive at the top of a file.
"use client";

import { ByteArkPlayerContainer } from "byteark-player-react";

export default ByteArkPlayerContainer;
  1. In component that you want to use ByteArkPlayer, dynamic load ByteArkPlayerContainer component
"use client"

import dynamic from "next/dynamic"

// Use nextjs dynamic to make sure that HTML Dom element is rendered before load player
const ByteArkPlayerContainer = dynamic(
  () => import("./byteark-player"),
  {
    // disable ssr (disable server-rendering)
    ssr: false,
    loading: () => (
      <div className="flex aspect-video size-full items-center justify-center bg-black p-0 px-2">
        <span className="text-center text-lg text-white"></span>
      </div>
    ),
  }
);
  1. Using ByteArkPlayerContainer client component in your component
export default function VideoPreviewPage() {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC'
      }
    ],
  }

  return <ByteArkPlayerContainer {...playerOptions} />
}

Full example with Next.js

// byteark-player.tsx
"use client";

import { ByteArkPlayerContainer } from "byteark-player-react";

export default ByteArkPlayerContainer;

// video-preview-page.tsx
"use client";
import React from 'react'
import { render } from 'react-dom'
import type { ByteArkPlayerContainerProps } from 'byteark-player-react'
import dynamic from "next/dynamic"

const ByteArkPlayerContainer = dynamic(
  () => import("./byteark-player"),
  {
    ssr: false,
    loading: () => (
      <div className="flex aspect-video size-full items-center justify-center bg-black p-0 px-2">
        <span className="text-center text-lg text-white"></span>
      </div>
    ),
  }
);

export default function VideoPreviewPage() {
  const playerOptions: ByteArkPlayerContainerProps = {
    autoplay: 'any',
    fluid: true,
    sources: [
      {
        src: 'https://byteark-playertzxedwv.stream-playlist.byteark.com/streams/TZyZheqEJUwC/playlist.m3u8',
        type: 'application/x-mpegURL',
        title: 'Big Buck Bunny',
        videoId: 'TZyZheqEJUwC'
      }
    ],
  }

  return <ByteArkPlayerContainer {...playerOptions} />
}

License

MIT © ByteArk Co. Ltd.

FAQs

Package last updated on 13 Nov 2024

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