|
1 | 1 | import type * as ExpoAV from 'expo-av'; |
2 | 2 | import type * as ExpoFS from 'expo-file-system'; |
3 | 3 | import type * as ExpoImageManipulator from 'expo-image-manipulator'; |
| 4 | +import type { EventSubscription } from 'expo-modules-core'; |
| 5 | +import type * as ExpoVideo from 'expo-video'; |
| 6 | +import type { StatusChangeEventPayload } from 'expo-video'; |
4 | 7 | import type * as ExpoVideoThumbnail from 'expo-video-thumbnails'; |
5 | | -import React from 'react'; |
| 8 | +import React, { useEffect } from 'react'; |
6 | 9 |
|
7 | | -import { getDownscaleSize } from '@sendbird/uikit-utils'; |
| 10 | +import { Logger, getDownscaleSize } from '@sendbird/uikit-utils'; |
8 | 11 |
|
9 | 12 | import SBUUtils from '../libs/SBUUtils'; |
10 | 13 | import expoBackwardUtils from '../utils/expoBackwardUtils'; |
11 | | -import type { MediaServiceInterface } from './types'; |
| 14 | +import type { ExpoVideoModule } from '../utils/expoBackwardUtils'; |
| 15 | +import type { MediaServiceInterface, VideoProps } from './types'; |
12 | 16 |
|
13 | 17 | type Modules = { |
14 | | - avModule: typeof ExpoAV; |
| 18 | + avModule: ExpoVideoModule; |
15 | 19 | thumbnailModule: typeof ExpoVideoThumbnail; |
16 | 20 | imageManipulator: typeof ExpoImageManipulator; |
17 | 21 | fsModule: typeof ExpoFS; |
18 | 22 | }; |
19 | 23 |
|
| 24 | +interface VideoModuleAdapter { |
| 25 | + VideoComponent: React.ComponentType<VideoProps>; |
| 26 | +} |
| 27 | + |
| 28 | +class LegacyExpoAVVideoAdapter implements VideoModuleAdapter { |
| 29 | + private readonly avModule: typeof ExpoAV; |
| 30 | + constructor(avModule: typeof ExpoAV) { |
| 31 | + this.avModule = avModule; |
| 32 | + } |
| 33 | + |
| 34 | + VideoComponent = ({ source, resizeMode, onLoad, ...props }: VideoProps) => { |
| 35 | + // FIXME: type error https://github.com/expo/expo/issues/17101 |
| 36 | + // @ts-ignore |
| 37 | + return <this.avModule.Video {...props} source={source} resizeMode={resizeMode} onLoad={onLoad} useNativeControls />; |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +class ExpoVideoAdapter implements VideoModuleAdapter { |
| 42 | + constructor(private readonly _videoModule: typeof ExpoVideo) {} |
| 43 | + |
| 44 | + VideoComponent = ({ source, resizeMode, onLoad, ...props }: VideoProps) => { |
| 45 | + const player = this._videoModule.useVideoPlayer(source); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (onLoad && player) { |
| 49 | + let subscription: EventSubscription | null = null; |
| 50 | + try { |
| 51 | + subscription = player.addListener('statusChange', (eventData: StatusChangeEventPayload) => { |
| 52 | + const { status, error } = eventData; |
| 53 | + if (status === 'readyToPlay' && !error) { |
| 54 | + onLoad(); |
| 55 | + } |
| 56 | + }); |
| 57 | + } catch (error) { |
| 58 | + const timeout = setTimeout(() => onLoad(), 300); |
| 59 | + return () => clearTimeout(timeout); |
| 60 | + } |
| 61 | + |
| 62 | + return () => { |
| 63 | + if (subscription) { |
| 64 | + subscription.remove(); |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + return undefined; |
| 69 | + }, [onLoad, player]); |
| 70 | + |
| 71 | + const getContentFit = (mode: typeof resizeMode): 'cover' | 'contain' | 'fill' => { |
| 72 | + switch (mode) { |
| 73 | + case 'cover': |
| 74 | + return 'cover'; |
| 75 | + case 'contain': |
| 76 | + return 'contain'; |
| 77 | + case 'stretch': |
| 78 | + return 'fill'; |
| 79 | + default: |
| 80 | + return 'contain'; |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + return React.createElement(this._videoModule.VideoView, { |
| 85 | + ...props, |
| 86 | + player, |
| 87 | + contentFit: getContentFit(resizeMode), |
| 88 | + }); |
| 89 | + }; |
| 90 | +} |
| 91 | + |
20 | 92 | const createExpoMediaService = ({ |
21 | 93 | avModule, |
22 | 94 | thumbnailModule, |
23 | 95 | imageManipulator, |
24 | 96 | fsModule, |
25 | 97 | }: Modules): MediaServiceInterface => { |
| 98 | + if (expoBackwardUtils.expoAV.isLegacyAVModule(avModule)) { |
| 99 | + Logger.warn( |
| 100 | + '[MediaService.Expo] expo-av is deprecated and will be removed in Expo 54. Please migrate to expo-video.', |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const videoAdapter = expoBackwardUtils.expoAV.isVideoModule(avModule) |
| 105 | + ? new ExpoVideoAdapter(avModule) |
| 106 | + : new LegacyExpoAVVideoAdapter(avModule); |
| 107 | + |
26 | 108 | return { |
27 | | - VideoComponent({ source, resizeMode, onLoad, ...props }) { |
28 | | - // FIXME: type error https://github.com/expo/expo/issues/17101 |
29 | | - // @ts-ignore |
30 | | - return <avModule.Video {...props} source={source} resizeMode={resizeMode} onLoad={onLoad} useNativeControls />; |
31 | | - }, |
| 109 | + VideoComponent: videoAdapter.VideoComponent, |
32 | 110 | async getVideoThumbnail({ url, quality, timeMills }) { |
33 | 111 | try { |
34 | 112 | const { uri } = await thumbnailModule.getThumbnailAsync(url, { quality, time: timeMills }); |
|
0 commit comments