Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active January 25, 2024 06:21
Show Gist options
  • Save yano3nora/a5bec12529bf7770e6756befc19e219d to your computer and use it in GitHub Desktop.
Save yano3nora/a5bec12529bf7770e6756befc19e219d to your computer and use it in GitHub Desktop.
[js: react-player] #js

Overview

github.com/cookpete/react-player

  • react でそのまま使える video, audio 再生プレイヤー
  • youtube などの映像サービスに広く対応
  • 普通に cdn に置いてあるファイルを読んだりとかも ok
$ npm i react-player
import React from 'react'
import ReactPlayer from 'react-player'
import { Center } from '@chakra-ui/react'
import { FaPlayCircle } from 'react-icons/fa'

export const Component = () => {
  const playerRef = React.useRef<ReactPlayer>(null)

  return <>
    <ReactPlayer
      ref={playerRef}
      url={'/videos/sample.mp4'}
      width={'100%'}
      height={'100%'}
      volume={0.5}
      light
      playing
      playIcon={<Center bg={'black'} w={'100%'} h={'100%'}>
        <FaPlayCircle fontSize={160} color={'white'} />
      </Center>}
      controls
      config={{
        file: {
          attributes: {
            onClick: () => {
              // https://github.com/cookpete/react-player?tab=readme-ov-file#instance-methods
              const player = playerRef.current?.getInternalPlayer() as HTMLVideoElement | undefined

              player?.paused
                ? player?.play()
                : player?.pause()
            },
            onContextMenu: (e: any) => e?.preventDefault(),
            controlsList: 'nofullscreen nodownload noremoteplayback',
            disablePictureInPicture: true,
          }
        }
      }}
    />
    <button onClick={() => playerRef.current?.seekTo(10)}>
      Seek to 00:10
    </button>
  </>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment