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