|
| 1 | +import { |
| 2 | + AutoCarousel, |
| 3 | + CarouselContextProvider, |
| 4 | + useAutoCarouselSlideIndex, |
| 5 | +} from '@strv/react-native-hero-carousel' |
| 6 | +import { SafeAreaView, StyleSheet, View, Text, Pressable, Dimensions, Platform } from 'react-native' |
| 7 | +import { useVideoPlayer, VideoView } from 'expo-video' |
| 8 | +import { LinearGradient } from 'expo-linear-gradient' |
| 9 | +import { useActiveSlideEffect, useIsActiveSlide } from '@/hooks/useActiveSlideEffect' |
| 10 | +import { useEffect, useRef, useState } from 'react' |
| 11 | +import { TimerPagination } from './components/TimerPagination' |
| 12 | +import { useEvent, useEventListener } from 'expo' |
| 13 | + |
| 14 | +const { width, height } = Dimensions.get('window') |
| 15 | +// Sample video URLs - these are publicly available videos that work well for testing |
| 16 | +const videos = [ |
| 17 | + 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4', |
| 18 | + 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4', |
| 19 | + 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4', |
| 20 | + 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', |
| 21 | + 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4', |
| 22 | +] |
| 23 | + |
| 24 | +const videoTitles = [ |
| 25 | + 'For Bigger Blazes', |
| 26 | + 'For Bigger Escapes', |
| 27 | + 'For Bigger Fun', |
| 28 | + 'Big Buck Bunny', |
| 29 | + 'Elephants Dream', |
| 30 | +] |
| 31 | + |
| 32 | +const Slide = ({ videoUri, title, index }: { videoUri: string; title: string; index: number }) => { |
| 33 | + const player = useVideoPlayer(videoUri) |
| 34 | + const { runAutoScroll } = useAutoCarouselSlideIndex() |
| 35 | + const isActiveSlide = useIsActiveSlide() |
| 36 | + const [duration, setDuration] = useState(0) |
| 37 | + useActiveSlideEffect(() => { |
| 38 | + player.currentTime = 0 |
| 39 | + player.play() |
| 40 | + return () => { |
| 41 | + player.pause() |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + const { isPlaying } = useEvent(player, 'playingChange', { isPlaying: player.playing }) |
| 46 | + |
| 47 | + useEventListener(player, 'statusChange', ({ status }) => { |
| 48 | + if (status === 'readyToPlay') { |
| 49 | + setDuration(player.duration) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + const intervalRef = useRef<ReturnType<typeof runAutoScroll> | null>(null) |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (isActiveSlide && duration) { |
| 57 | + intervalRef.current = runAutoScroll(duration * 1000) |
| 58 | + } |
| 59 | + }, [isActiveSlide, duration, runAutoScroll]) |
| 60 | + |
| 61 | + return ( |
| 62 | + <View style={styles.slide}> |
| 63 | + <Pressable |
| 64 | + key={index} |
| 65 | + style={styles.slide} |
| 66 | + onPress={() => { |
| 67 | + if (isPlaying) { |
| 68 | + player.pause() |
| 69 | + intervalRef.current?.pause() |
| 70 | + } else { |
| 71 | + player.play() |
| 72 | + intervalRef.current?.resume() |
| 73 | + } |
| 74 | + }} |
| 75 | + > |
| 76 | + <VideoView |
| 77 | + player={player} |
| 78 | + style={styles.video} |
| 79 | + contentFit={Platform.OS === 'android' ? 'fill' : 'cover'} |
| 80 | + nativeControls={false} |
| 81 | + /> |
| 82 | + <LinearGradient colors={['rgba(0,0,0,0.8)', 'transparent']} style={styles.topGradient} /> |
| 83 | + <LinearGradient colors={['transparent', 'rgba(0,0,0,0.8)']} style={styles.gradient}> |
| 84 | + <Text style={styles.title}>{title}</Text> |
| 85 | + <Text style={styles.subtitle}>Swipe to navigate • Tap to play/pause</Text> |
| 86 | + </LinearGradient> |
| 87 | + </Pressable> |
| 88 | + </View> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +export default function VideoCarouselExample() { |
| 93 | + return ( |
| 94 | + <CarouselContextProvider> |
| 95 | + <SafeAreaView style={styles.container}> |
| 96 | + <View style={styles.container}> |
| 97 | + <AutoCarousel disableAutoScroll={true}> |
| 98 | + {videos.map((video, index) => ( |
| 99 | + <Slide key={index} videoUri={video} title={videoTitles[index]} index={index} /> |
| 100 | + ))} |
| 101 | + </AutoCarousel> |
| 102 | + <TimerPagination total={videos.length} hideProgressOnInteraction={false} /> |
| 103 | + </View> |
| 104 | + </SafeAreaView> |
| 105 | + </CarouselContextProvider> |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +const styles = StyleSheet.create({ |
| 110 | + container: { |
| 111 | + flex: 1, |
| 112 | + backgroundColor: '#000', |
| 113 | + }, |
| 114 | + slide: { |
| 115 | + flex: 1, |
| 116 | + width: '100%', |
| 117 | + height: '100%', |
| 118 | + overflow: 'hidden', |
| 119 | + }, |
| 120 | + video: { |
| 121 | + width: width, |
| 122 | + height: height, |
| 123 | + }, |
| 124 | + gradient: { |
| 125 | + position: 'absolute', |
| 126 | + bottom: 0, |
| 127 | + left: 0, |
| 128 | + right: 0, |
| 129 | + height: '50%', |
| 130 | + justifyContent: 'flex-end', |
| 131 | + padding: 20, |
| 132 | + }, |
| 133 | + topGradient: { |
| 134 | + position: 'absolute', |
| 135 | + top: 0, |
| 136 | + left: 0, |
| 137 | + right: 0, |
| 138 | + height: '20%', |
| 139 | + }, |
| 140 | + title: { |
| 141 | + fontSize: 32, |
| 142 | + bottom: 60, |
| 143 | + left: 20, |
| 144 | + position: 'absolute', |
| 145 | + lineHeight: 32, |
| 146 | + fontWeight: 'bold', |
| 147 | + color: 'white', |
| 148 | + }, |
| 149 | + subtitle: { |
| 150 | + fontSize: 16, |
| 151 | + bottom: 20, |
| 152 | + left: 20, |
| 153 | + position: 'absolute', |
| 154 | + lineHeight: 20, |
| 155 | + color: 'white', |
| 156 | + opacity: 0.8, |
| 157 | + }, |
| 158 | +}) |
0 commit comments