import React, { useEffect } from 'react'; import { useEventListener } from 'expo'; import { AudioComponent } from './AudioVideo'; let videoPackage; try { videoPackage = require('expo-video'); } catch (e) { // do nothing } if (!videoPackage) { console.log( 'The video library is currently not installed. To allow in-app video playback, install the "expo-video" package.', ); } const VideoComponent = videoPackage?.VideoView; const useVideoPlayer = videoPackage?.useVideoPlayer; let Video = null; /** * Tuned for chat gallery clips. expo-video's defaults are way too * generous (Android: 20s read ahead, no byte cap and ExoPlayer * picks based on bitrate). These values cap each player instance at roughly * 5-6 MB while still giving a comfortable margin against network stalls. * Integrators with different needs can replace this wrapper via * `registerNativeHandlers({ Video: ... })`. * * Per expo-video docs the whole object must be assigned at once and setting * individual properties on `player.bufferOptions` is not supported. */ const BUFFER_OPTIONS = { maxBufferBytes: 6_000_000, // Android only, ~6 MB hard cap minBufferForPlayback: 1, // Android only, seconds before playback can start preferredForwardBufferDuration: 10, // both platforms, seconds }; // expo-video if (videoPackage) { Video = ({ onLoadStart, onLoad, onEnd, onProgress, onBuffer, resizeMode, style, uri, videoRef, }) => { const player = useVideoPlayer(uri, (player) => { player.timeUpdateEventInterval = 0.5; player.bufferOptions = BUFFER_OPTIONS; videoRef.current = player; }); useEventListener(player, 'statusChange', ({ status, oldStatus }) => { if ((!oldStatus || oldStatus === 'idle') && status === 'loading') { onLoadStart(); } else if ((oldStatus === 'loading' || oldStatus === 'idle') && status === 'readyToPlay') { onLoad({ duration: player.duration }); onBuffer({ buffering: false }); } else if (oldStatus === 'readyToPlay' && status === 'loading') { onBuffer({ buffering: true }); } }); useEventListener(player, 'playToEnd', () => { player.replay(); onEnd(); }); useEventListener(player, 'timeUpdate', ({ currentTime }) => onProgress({ currentTime, seekableDuration: player.duration }), ); // This is done so that the audio of the video is not muted when the phone is in silent mode for iOS. useEffect(() => { const initializeSound = async () => { if (AudioComponent) { await AudioComponent.setAudioModeAsync({ playsInSilentModeIOS: true, }); } }; initializeSound(); }, []); return ( ); }; } export { Video };