Urgent.News

What's breaking now, across thousands of outlets.

Tech

Create a Video player with react-player which supports mp4, Youtube and Vimeo videos with ReactJS

First, let’s install the library: npm install react - player Next, let’s create our video player component: import React from ' react ' ; import ReactPlayer from ' react-player ' ; const VideoPlayer = ({ url }) => { const isYoutube = url . includes ( ' youtube.com ' ); const isVimeo = url . includes ( ' vimeo.com ' ); if ( isYoutube ) { const videoId = url . split ( ' v= ' )[ 1 ]; return ( <…

To construct a video player utilizing the react-player library that supports playback of MP4, YouTube, and Vimeo videos within a ReactJS framework, you must first install the library using the command npm install react-player. Once installed, commence by crafting a VideoPlayer component. Import React and ReactPlayer, and define the component with a prop named url.

Next, ascertain whether the provided URL points to a YouTube or Vimeo video by examining if the string "youtube.com" or "vimeo.com" appears within the URL string. If the URL contains "youtube.com", employ JavaScript's split method to isolate the video ID, which follows the pattern "v=VIDEO_ID". Then, within the ReactPlayer component, assign the constructed URL using the format "https://www.youtube.com/watch?v=VIDEO_ID". Apply width and height properties set to 100% to ensure the player occupies the entire container space.

Similarly, for Vimeo videos, isolate the video ID from the URL using "vimeo.com/VIDEO_ID". Once again, insert this URL into the ReactPlayer component, maintaining the 100% width and height for consistent sizing.

If the URL does not correspond to either YouTube or Vimeo, simply render the ReactPlayer component with the supplied URL and incorporate the controls prop to enable video controls. Additionally, set the width and height properties to 100% to maintain uniform playback dimensions across all video types.

Finally, integrate the VideoPlayer component into your application. Import React and the VideoPlayer component, and utilize it within your App component. Supply diverse video URLs to illustrate the component's capability in playing YouTube, Vimeo, and MP4 videos. These instances can be observed in the provided code snippet, demonstrating the functionality in practice. For a live demonstration, access the code sandbox at https://codesandbox.io/s/reactplayerdemo-mokjcd.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

CSS Container Style Queries: Let the Parent Set the Theme

Every design system eventually grows a modifier class problem. The card needs a dark version for the footer, a compact version for the sidebar, and a "highlighted" version for the pricing page.

  • Container style queries let parents set custom properties influencing descendants' styles.
  • @container rule with style function checks computed values of custom properties on ancestors.
  • Range queries added in 2023 enable styling based on numeric values, improving nuance.

More from Monday 12 October →