import React, {useEffect, useRef, useState} from 'react' import {View} from 'react-native' import Hls from 'hls.js' import {atoms as a} from '#/alf' import {Controls} from './VideoWebControls' export function VideoEmbedInner({ source, active, setActive, onScreen, }: { source: string active: boolean setActive: () => void onScreen: boolean }) { const containerRef = useRef(null) const ref = useRef(null) const [focused, setFocused] = useState(false) const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false) const hlsRef = useRef(undefined) useEffect(() => { if (!ref.current) return if (!Hls.isSupported()) throw new HLSUnsupportedError() const hls = new Hls({capLevelToPlayerSize: true}) hlsRef.current = hls hls.attachMedia(ref.current) hls.loadSource(source) // initial value, later on it's managed by Controls hls.autoLevelCapping = 0 hls.on(Hls.Events.SUBTITLE_TRACKS_UPDATED, (event, data) => { if (data.subtitleTracks.length > 0) { setHasSubtitleTrack(true) } }) return () => { hlsRef.current = undefined hls.detachMedia() hls.destroy() } }, [source]) return (
) } export class HLSUnsupportedError extends Error { constructor() { super('HLS is not supported') } }