-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(videoJs): implement videoJs to story video (#1068)
* feat(videoJs): implement videoJs to story video * refactor(video): add poster
- Loading branch information
1 parent
4f97916
commit d698404
Showing
16 changed files
with
23,413 additions
and
151 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,3 @@ | |
|
||
.presentationVideo | ||
padding-bottom: 15vh | ||
|
||
.youtubePlayer | ||
width: 100% | ||
height: 100% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, {FunctionComponent, useEffect, useRef} from 'react'; | ||
import videojs, {VideoJsPlayerOptions} from 'video.js'; | ||
import {getStoryAssetUrl} from '../../../libs/get-story-asset-urls'; | ||
|
||
import {Language} from '../../../types/language'; | ||
|
||
import 'video.js/dist/video-js.css'; | ||
|
||
interface Props { | ||
storyId: string; | ||
videoSrc: string; | ||
language: Language; | ||
isStoryMode: boolean; | ||
videoCaptions?: string; | ||
videoPoster?: string; | ||
} | ||
|
||
const VideoJS: FunctionComponent<Props> = ({ | ||
storyId, | ||
videoSrc, | ||
language, | ||
isStoryMode, | ||
videoCaptions, | ||
videoPoster | ||
}) => { | ||
const videoRef = useRef(null); | ||
const playerRef = useRef<videojs.Player | null>(); | ||
const videoUrl = videoSrc && getStoryAssetUrl(storyId, videoSrc); | ||
const captionsUrl = videoCaptions && getStoryAssetUrl(storyId, videoCaptions); | ||
const posterUrl = videoPoster && getStoryAssetUrl(storyId, videoPoster); | ||
|
||
const videoJsOptions: VideoJsPlayerOptions = { | ||
autoplay: isStoryMode ? false : true, | ||
controls: true, | ||
responsive: true, | ||
fluid: true, | ||
aspectRatio: '4:3', | ||
poster: posterUrl, | ||
sources: [ | ||
{ | ||
src: videoUrl, | ||
type: 'video/mp4' | ||
} | ||
], | ||
tracks: [ | ||
{ | ||
srclang: language, | ||
kind: 'captions', | ||
src: captionsUrl, | ||
default: true | ||
} | ||
] | ||
}; | ||
|
||
const handlePlayerReady = (player: videojs.Player) => { | ||
playerRef.current = player; | ||
}; | ||
|
||
useEffect(() => { | ||
// make sure Video.js player is only initialized once | ||
if (!playerRef.current) { | ||
const videoElement = videoRef.current; | ||
if (!videoElement) { | ||
return; | ||
} | ||
|
||
const player: videojs.Player = (playerRef.current = videojs( | ||
videoElement, | ||
videoJsOptions, | ||
() => handlePlayerReady(player) | ||
)); | ||
} | ||
}, [videoJsOptions, videoRef]); | ||
|
||
// Dispose the Video.js player when component unmounts | ||
useEffect(() => { | ||
const player = playerRef.current; | ||
|
||
return () => { | ||
if (player) { | ||
player.dispose(); | ||
playerRef.current = null; | ||
} | ||
}; | ||
}, [playerRef]); | ||
|
||
return ( | ||
<div data-vjs-player> | ||
<video | ||
ref={videoRef} | ||
className="video-js vjs-big-play-centered" | ||
style={{height: '100%'}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default VideoJS; |
3 changes: 3 additions & 0 deletions
3
src/scripts/components/stories/youtube-player/youtube-player.styl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.videoPlayer | ||
width: 100% | ||
height: 100% |
54 changes: 54 additions & 0 deletions
54
src/scripts/components/stories/youtube-player/youtube-player.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable camelcase */ | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
import React, {FunctionComponent} from 'react'; | ||
import YouTube, {Options} from 'react-youtube'; | ||
import {YouTubePlayer} from 'youtube-player/dist/types'; | ||
|
||
import {Language} from '../../../types/language'; | ||
|
||
import styles from './youtube-player.styl'; | ||
|
||
interface Props { | ||
videoId?: string; | ||
language: Language; | ||
isStoryMode: boolean; | ||
onPlay: (player: YouTubePlayer) => void; | ||
} | ||
|
||
const YoutubePlayer: FunctionComponent<Props> = ({ | ||
videoId, | ||
language, | ||
isStoryMode, | ||
onPlay | ||
}) => { | ||
const options: Options = { | ||
height: '100%', | ||
width: '100%', | ||
playerVars: { | ||
rel: 0, | ||
cc_load_policy: 1, | ||
hl: language, | ||
// @ts-ignore | ||
cc_lang_pref: language, | ||
color: 'red', | ||
controls: 2, | ||
iv_load_policy: 3, | ||
modestbranding: 1, | ||
showinfo: 0, | ||
allow: | ||
'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' | ||
} | ||
}; | ||
|
||
return ( | ||
<YouTube | ||
containerClassName={styles.videoPlayer} | ||
videoId={videoId} | ||
opts={options} | ||
onReady={event => !isStoryMode && event.target.playVideo()} | ||
onPlay={event => onPlay(event.target)} | ||
/> | ||
); | ||
}; | ||
|
||
export default YoutubePlayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.