Skip to content

Commit

Permalink
Remove url resolving from video player
Browse files Browse the repository at this point in the history
  • Loading branch information
WRadoslaw committed Feb 28, 2024
1 parent 74a92f1 commit 7131db5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
useState,
} from 'react'
import useResizeObserver from 'use-resize-observer'
import { VideoJsPlayer } from 'video.js'

import { useFullVideo } from '@/api/hooks/video'
import { FullVideoFieldsFragment } from '@/api/queries/__generated__/fragments.generated'
Expand Down Expand Up @@ -69,7 +68,7 @@ import {
} from './VideoPlayer.styles'
import { VideoShare } from './VideoShare'
import { CustomVideojsEvents, PlayerState, VOLUME_STEP, hotkeysHandler, isFullScreenEnabled } from './utils'
import { VideoJsConfig, useVideoJsPlayer } from './videoJsPlayer'
import { VideoJsConfig, VideoJsPlayer, useVideoJsPlayer } from './videoJsPlayer'

export type VideoPlayerProps = {
channelAvatarUrls?: string[] | null
Expand Down Expand Up @@ -781,6 +780,7 @@ const VideoPlayerComponent: ForwardRefRenderFunction<HTMLVideoElement, VideoPlay
}
}}
>
<video style={videoStyle} ref={playerRef} className="video-js" onClick={onVideoClick} />
{needsManualPlay && (
<BigPlayButtonContainer
onClick={() => {
Expand All @@ -792,7 +792,6 @@ const VideoPlayerComponent: ForwardRefRenderFunction<HTMLVideoElement, VideoPlay
</BigPlayButton>
</BigPlayButtonContainer>
)}
<video style={videoStyle} ref={playerRef} className="video-js" onClick={onVideoClick} />
{showPlayerControls && (
<>
<ControlsOverlay isSettingsPopoverOpened={isSettingsPopoverOpened} elevated={isFullScreen}>
Expand Down
21 changes: 11 additions & 10 deletions packages/atlas/src/components/_video/VideoPlayer/videoJsPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RefObject, useEffect, useRef, useState } from 'react'
import { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js'
import videojs from 'video.js/dist/alt/video.core.novtt'

import { useGetAssetUrl } from '@/hooks/useGetAssetUrl'
Expand All @@ -21,6 +20,7 @@ export type VideoJsConfig = {
onTimeUpdated?: (time: number) => void
}

export type VideoJsPlayer = videojs.Player
type VideoJsPlayerHook = (config: VideoJsConfig) => [VideoJsPlayer | null, RefObject<HTMLVideoElement>]
export const useVideoJsPlayer: VideoJsPlayerHook = ({
fill,
Expand All @@ -40,13 +40,12 @@ export const useVideoJsPlayer: VideoJsPlayerHook = ({
}) => {
const playerRef = useRef<HTMLVideoElement | null>(null)
const [player, setPlayer] = useState<VideoJsPlayer | null>(null)
const { url: src } = useGetAssetUrl(videoUrls, 'video')
const { url: posterUrl } = useGetAssetUrl(posterUrls, 'cover')
useEffect(() => {
if (!playerRef.current) {
return
}
const videoJsOptions: VideoJsPlayerOptions = {
const videoJsOptions: videojs.PlayerOptions = {
controls: true,
playsinline: true,
bigPlayButton: false,
Expand All @@ -55,7 +54,6 @@ export const useVideoJsPlayer: VideoJsPlayerHook = ({
}

const playerInstance = videojs(playerRef.current as Element, videoJsOptions)

setPlayer(playerInstance)

return () => {
Expand All @@ -70,14 +68,17 @@ export const useVideoJsPlayer: VideoJsPlayerHook = ({
}, [])

useEffect(() => {
if (!player || !src) {
if (!player || !videoUrls) {
return
}
player.src({
src: src,
type: 'video/mp4',
})
}, [player, src])

player.src(
videoUrls.map((url) => ({
src: url,
type: 'video/mp4',
}))
)
}, [player, videoUrls])

useEffect(() => {
if (!player || !width) {
Expand Down

0 comments on commit 7131db5

Please sign in to comment.