@@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
22
33import { useEventListener } from 'expo' ;
44
5- import { AudioComponent } from './AudioVideo' ;
5+ import { AudioComponent , VideoComponent as ExpoAVVideoComponent } from './AudioVideo' ;
66
77let videoPackage ;
88
@@ -18,56 +18,98 @@ if (!videoPackage) {
1818 ) ;
1919}
2020
21- const VideoComponent = videoPackage ? videoPackage . VideoView : null ;
21+ const VideoComponent = videoPackage ? videoPackage . VideoView : ExpoAVVideoComponent ;
2222const useVideoPlayer = videoPackage ? videoPackage . useVideoPlayer : null ;
2323
24- export const Video = videoPackage
25- ? ( { onLoadStart, onLoad, onEnd, onProgress, onBuffer, resizeMode, style, uri, videoRef } ) => {
26- const player = useVideoPlayer ( uri , ( player ) => {
27- player . timeUpdateEventInterval = 0.5 ;
28- videoRef . current = player ;
29- } ) ;
24+ let Video = null ;
3025
31- useEventListener ( player , 'statusChange' , ( { status, oldStatus } ) => {
32- if ( ( ! oldStatus || oldStatus === 'idle' ) && status === 'loading' ) {
33- onLoadStart ( ) ;
34- } else if ( ( oldStatus === 'loading' || oldStatus === 'idle' ) && status === 'readyToPlay' ) {
35- onLoad ( { duration : player . duration } ) ;
36- onBuffer ( { buffering : false } ) ;
37- } else if ( oldStatus === 'readyToPlay' && status === 'loading' ) {
38- onBuffer ( { buffering : true } ) ;
39- }
40- } ) ;
26+ // expo-video
27+ if ( videoPackage ) {
28+ Video = ( {
29+ onLoadStart,
30+ onLoad,
31+ onEnd,
32+ onProgress,
33+ onBuffer,
34+ resizeMode,
35+ style,
36+ uri,
37+ videoRef,
38+ } ) => {
39+ const player = useVideoPlayer ( uri , ( player ) => {
40+ player . timeUpdateEventInterval = 0.5 ;
41+ videoRef . current = player ;
42+ } ) ;
43+
44+ useEventListener ( player , 'statusChange' , ( { status, oldStatus } ) => {
45+ if ( ( ! oldStatus || oldStatus === 'idle' ) && status === 'loading' ) {
46+ onLoadStart ( ) ;
47+ } else if ( ( oldStatus === 'loading' || oldStatus === 'idle' ) && status === 'readyToPlay' ) {
48+ onLoad ( { duration : player . duration } ) ;
49+ onBuffer ( { buffering : false } ) ;
50+ } else if ( oldStatus === 'readyToPlay' && status === 'loading' ) {
51+ onBuffer ( { buffering : true } ) ;
52+ }
53+ } ) ;
4154
42- useEventListener ( player , 'playToEnd' , ( ) => {
43- player . replay ( ) ;
44- onEnd ( ) ;
45- } ) ;
55+ useEventListener ( player , 'playToEnd' , ( ) => {
56+ player . replay ( ) ;
57+ onEnd ( ) ;
58+ } ) ;
4659
47- useEventListener ( player , 'timeUpdate' , ( { currentTime } ) =>
48- onProgress ( { currentTime, seekableDuration : player . duration } ) ,
49- ) ;
60+ useEventListener ( player , 'timeUpdate' , ( { currentTime } ) =>
61+ onProgress ( { currentTime, seekableDuration : player . duration } ) ,
62+ ) ;
63+
64+ // This is done so that the audio of the video is not muted when the phone is in silent mode for iOS.
65+ useEffect ( ( ) => {
66+ const initializeSound = async ( ) => {
67+ if ( AudioComponent ) {
68+ await AudioComponent . setAudioModeAsync ( {
69+ playsInSilentModeIOS : true ,
70+ } ) ;
71+ }
72+ } ;
73+ initializeSound ( ) ;
74+ } , [ ] ) ;
5075
51- // This is done so that the audio of the video is not muted when the phone is in silent mode for iOS.
52- useEffect ( ( ) => {
53- const initializeSound = async ( ) => {
54- if ( AudioComponent ) {
55- await AudioComponent . setAudioModeAsync ( {
56- playsInSilentModeIOS : true ,
57- } ) ;
58- }
59- } ;
60- initializeSound ( ) ;
61- } , [ ] ) ;
76+ return (
77+ < VideoComponent
78+ allowsFullscreen
79+ contentFit = { resizeMode }
80+ nativeControls = { false }
81+ player = { player }
82+ style = { [ style ] }
83+ />
84+ ) ;
85+ } ;
86+ }
87+ // expo-av
88+ else if ( ExpoAVVideoComponent ) {
89+ Video = ( { onPlaybackStatusUpdate, paused, resizeMode, style, uri, videoRef } ) => {
90+ // This is done so that the audio of the video is not muted when the phone is in silent mode for iOS.
91+ useEffect ( ( ) => {
92+ const initializeSound = async ( ) => {
93+ await AudioComponent . setAudioModeAsync ( {
94+ playsInSilentModeIOS : true ,
95+ } ) ;
96+ } ;
97+ initializeSound ( ) ;
98+ } , [ ] ) ;
99+
100+ return (
101+ < VideoComponent
102+ onPlaybackStatusUpdate = { onPlaybackStatusUpdate }
103+ ref = { videoRef }
104+ resizeMode = { resizeMode }
105+ shouldPlay = { ! paused }
106+ source = { {
107+ uri,
108+ } }
109+ style = { [ style ] }
110+ />
111+ ) ;
112+ } ;
113+ }
62114
63- return (
64- < VideoComponent
65- allowsFullscreen
66- contentFit = { resizeMode }
67- nativeControls = { false }
68- player = { player }
69- style = { [ style ] }
70- />
71- ) ;
72- }
73- : null ;
115+ export { Video } ;
0 commit comments