cookpete / react-player

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
https://cookpete.github.io/react-player
MIT License
9.26k stars 1.15k forks source link

Videos fully contained and centred in squared react-player component #1003

Closed FilipMasar closed 4 years ago

FilipMasar commented 4 years ago

I have square react-player component

<div
    style={{
        position: "relative",
        paddingTop: "100%",
        borderStyle: "solid"
    }}
>
    <ReactPlayer
        style={{ position: "absolute", top: 0, left: 0 }}
        url={url}
        controls
        width="100%"
        height="100%"
    />
</div>

But some of my videos are not squared. I would like to have a squared react-player component, and show videos fully contained and centred in it. That means left and right parts of the video won't be shown. Is it possible ?

image Thanks for your help ;-)

cookpete commented 4 years ago

Try adding object-fit: cover; to the <video> element (use config.file.attributes to set props on the video element).

Browser support: https://caniuse.com/#feat=object-fit

FilipMasar commented 4 years ago

Thanks for your quick response. I've done this, but it also stretches react-player itself

<ReactPlayer
  style={{ position: "absolute", top: 0, left: 0 }}
  url={url}
  controls
  width="100%"
  height="100%"
  config={{
    file: {
      attributes: {
        style: { height: "100%", objectFit: "cover" },
      },
    },
  }}
/>;

How do I pass the style to <video> element ?

Now it looks like this image

cookpete commented 4 years ago

Have you tried width: "100%" as well? eg style: { width: "100%", height: "100%", objectFit: "cover" }

FilipMasar commented 4 years ago

That was the missing part 😅 Thanks so much for help :-)

This is the solution

<div
  style={{
    position: "relative",
    paddingTop: "100%",
  }}
>
  <ReactPlayer
    style={{ position: "absolute", top: 0, left: 0 }}
    url={videoURL}
    controls
    width="100%"
    height="100%"
    config={{
      file: {
        attributes: {
          style: {
            width: "100%",
            height: "100%",
            objectFit: "cover",
          },
        },
      },
    }}
  />
</div>
atsakiridis commented 1 week ago

@cookpete, can you think of any reason why this wouldn't work in a Vimeo player, i.e.

   ...
  <ReactPlayer
    style={{ position: "absolute", top: 0, left: 0 }}
    url={vimeoVideoURL}
    controls
    width="100%"
    height="100%"
    config={{
      vimeo: {
        attributes: {
          style: {
            width: "100%",
            height: "100%",
            objectFit: "cover",
          },
        },
      },
    }}
  />
</div>