-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.js
More file actions
97 lines (79 loc) · 2.62 KB
/
Copy pathApp.js
File metadata and controls
97 lines (79 loc) · 2.62 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
import React, { useState } from 'react';
import { StyleSheet, Text, View, Dimensions, Button } from 'react-native';
import { Video } from 'expo-av'
import * as FileSystem from 'expo-file-system';
const { width, height } = Dimensions.get('window');
export default function App() {
const [videoUrl, setVideoUrl] = useState('https://www.radiantmediaplayer.com/media/bbb-360p.mp4')
const [buttonTitle, setButtonTitle] = useState('Download')
const [progressValue, setProgressValue] = useState(0)
const [totalSize, setTotalSize] = useState(0)
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
// async function getVideoUrl() {
// let tmp = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'small.mp4');
// let url = tmp.exists ? FileSystem.documentDirectory + 'small.mp4' : videoUrl
// return url
// }
// getVideoUrl().then((url) => {
// setVideoUrl(url)
// })
async function downloadVideo() {
setButtonTitle('Downloading')
const callback = downloadProgress => {
setTotalSize(formatBytes(downloadProgress.totalBytesExpectedToWrite))
var progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
progress = progress.toFixed(2) * 100
setProgressValue(progress.toFixed(0))
};
const downloadResumable = FileSystem.createDownloadResumable(
videoUrl,
FileSystem.documentDirectory + 'small.mp4',
{},
callback
);
try {
const { uri } = await downloadResumable.downloadAsync();
console.log('Finished downloading to ', uri);
setButtonTitle('Downloaded')
} catch (e) {
console.error(e);
}
}
return (
<View style={styles.container}>
<Video
source={{ uri: videoUrl }}
rate={1.0}
volume={1.0}
isMuted={false}
resizeMode="cover"
shouldPlay={false}
isLooping={false}
useNativeControls
style={styles.video}
/>
<Button title={buttonTitle} onPress={downloadVideo}></Button>
<Text> Size: {totalSize} </Text>
<Text>Progress: {progressValue} %</Text>
</View>
);
}
const styles = StyleSheet.create({
video: {
width: width,
height: height / 3
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});