/
.VideoManager.js.txt
68 lines (60 loc) · 2.01 KB
/
.VideoManager.js.txt
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
// VideoManager.js
import React, { useState, useEffect } from 'react';
import { View, Text, Button, Alert } from 'react-native';
import * as FileSystem from 'expo-file-system';
import * as Permissions from 'expo-permissions';
import MMKV from 'react-native-mmkv';
import { Video } from 'react-native-video';
// Initialize MMKV before using it
MMKV.initialize();
const VideoManager = () => {
const [videoPath, setVideoPath] = useState('');
useEffect(() => {
// Load the video path from MMKV when the component mounts
const storedVideoPath = MMKV.getString('videoPath');
setVideoPath(storedVideoPath || '');
}, []);
const saveVideoLink = async () => {
try {
// Download the video
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status === 'granted') {
const downloadOptions = {
fileCache: true,
path: FileSystem.documentDirectory + 'downloadedVideo.mp4',
};
const downloadedFile = await FileSystem.downloadAsync(
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
downloadOptions
);
// Save the video path to MMKV
MMKV.set('videoPath', downloadedFile.uri);
setVideoPath(downloadedFile.uri);
Alert.alert('Video Downloaded and Stored Successfully!');
} else {
Alert.alert('Permission to access camera roll denied.');
}
} catch (error) {
console.error('Error downloading video:', error);
Alert.alert('Error downloading video. Please try again.');
}
};
return (
<View>
<Button title="Save Video Link" onPress={saveVideoLink} />
{videoPath ? (
<View>
<Text>Stored Video Path:</Text>
<Text>{videoPath}</Text>
<Video
source={{ uri: videoPath }}
style={{ width: 300, height: 300 }}
controls
resizeMode="contain"
/>
</View>
) : null}
</View>
);
};
export default VideoManager;