pass tracks to controls
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, {useEffect, useId, useRef, useState} from 'react'
|
import React, {useEffect, useId, useRef, useState} from 'react'
|
||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
import {AppBskyEmbedVideo} from '@atproto/api'
|
import {AppBskyEmbedVideo} from '@atproto/api'
|
||||||
import Hls from 'hls.js'
|
import Hls, {MediaPlaylist} from 'hls.js'
|
||||||
|
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a} from '#/alf'
|
||||||
import {Controls} from './VideoWebControls'
|
import {Controls} from './VideoWebControls'
|
||||||
@@ -20,7 +20,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
const containerRef = useRef<HTMLDivElement>(null)
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
const ref = useRef<HTMLVideoElement>(null)
|
const ref = useRef<HTMLVideoElement>(null)
|
||||||
const [focused, setFocused] = useState(false)
|
const [focused, setFocused] = useState(false)
|
||||||
const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false)
|
const [subtitleTracks, setSubtitleTracks] = useState<MediaPlaylist[]>([])
|
||||||
const figId = useId()
|
const figId = useId()
|
||||||
|
|
||||||
// send error up to error boundary
|
// send error up to error boundary
|
||||||
@@ -46,7 +46,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
|
|
||||||
hls.on(Hls.Events.SUBTITLE_TRACKS_UPDATED, (_event, data) => {
|
hls.on(Hls.Events.SUBTITLE_TRACKS_UPDATED, (_event, data) => {
|
||||||
if (data.subtitleTracks.length > 0) {
|
if (data.subtitleTracks.length > 0) {
|
||||||
setHasSubtitleTrack(true)
|
setSubtitleTracks(data.subtitleTracks)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
setFocused={setFocused}
|
setFocused={setFocused}
|
||||||
onScreen={onScreen}
|
onScreen={onScreen}
|
||||||
fullscreenRef={containerRef}
|
fullscreenRef={containerRef}
|
||||||
hasSubtitleTrack={hasSubtitleTrack}
|
subtitleTracks={subtitleTracks}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {SvgProps} from 'react-native-svg'
|
|||||||
import {msg, Trans} from '@lingui/macro'
|
import {msg, Trans} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import type Hls from 'hls.js'
|
import type Hls from 'hls.js'
|
||||||
|
import {MediaPlaylist} from 'hls.js'
|
||||||
|
|
||||||
import {isFirefox} from '#/lib/browser'
|
import {isFirefox} from '#/lib/browser'
|
||||||
import {clamp} from '#/lib/numbers'
|
import {clamp} from '#/lib/numbers'
|
||||||
@@ -42,7 +43,7 @@ export function Controls({
|
|||||||
setFocused,
|
setFocused,
|
||||||
onScreen,
|
onScreen,
|
||||||
fullscreenRef,
|
fullscreenRef,
|
||||||
hasSubtitleTrack,
|
subtitleTracks,
|
||||||
}: {
|
}: {
|
||||||
videoRef: React.RefObject<HTMLVideoElement>
|
videoRef: React.RefObject<HTMLVideoElement>
|
||||||
hlsRef: React.RefObject<Hls | undefined>
|
hlsRef: React.RefObject<Hls | undefined>
|
||||||
@@ -52,7 +53,7 @@ export function Controls({
|
|||||||
setFocused: (focused: boolean) => void
|
setFocused: (focused: boolean) => void
|
||||||
onScreen: boolean
|
onScreen: boolean
|
||||||
fullscreenRef: React.RefObject<HTMLDivElement>
|
fullscreenRef: React.RefObject<HTMLDivElement>
|
||||||
hasSubtitleTrack: boolean
|
subtitleTracks: MediaPlaylist[]
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
play,
|
play,
|
||||||
@@ -128,12 +129,12 @@ export function Controls({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hlsRef.current) return
|
if (!hlsRef.current) return
|
||||||
if (hasSubtitleTrack && subtitlesEnabled && canPlay) {
|
if (subtitleTracks.length > 0 && subtitlesEnabled && canPlay) {
|
||||||
hlsRef.current.subtitleTrack = 0
|
hlsRef.current.subtitleTrack = 0
|
||||||
} else {
|
} else {
|
||||||
hlsRef.current.subtitleTrack = -1
|
hlsRef.current.subtitleTrack = -1
|
||||||
}
|
}
|
||||||
}, [hasSubtitleTrack, subtitlesEnabled, hlsRef, canPlay])
|
}, [subtitleTracks, subtitlesEnabled, hlsRef, canPlay])
|
||||||
|
|
||||||
// clicking on any button should focus the player, if it's not already focused
|
// clicking on any button should focus the player, if it's not already focused
|
||||||
const drawFocus = useCallback(() => {
|
const drawFocus = useCallback(() => {
|
||||||
@@ -324,7 +325,7 @@ export function Controls({
|
|||||||
<Text style={{color: t.palette.white}}>
|
<Text style={{color: t.palette.white}}>
|
||||||
{formatTime(currentTime)} / {formatTime(duration)}
|
{formatTime(currentTime)} / {formatTime(duration)}
|
||||||
</Text>
|
</Text>
|
||||||
{hasSubtitleTrack && (
|
{subtitleTracks.length > 0 && (
|
||||||
<ControlButton
|
<ControlButton
|
||||||
active={subtitlesEnabled}
|
active={subtitlesEnabled}
|
||||||
activeLabel={_(msg`Disable subtitles`)}
|
activeLabel={_(msg`Disable subtitles`)}
|
||||||
|
|||||||
Reference in New Issue
Block a user