gpt4 book ai didi

react-native - 如何在 react-native-video 中实现缓存

转载 作者:行者123 更新时间:2023-12-02 09:19:11 26 4
gpt4 key购买 nike

我们如何在 react-native-video 中实现缓存?基本上,当视频当前从网络资源流式传输时,我们如何将视频保存在某个地方,然后在访问相同资源时检索它。最好的方法是什么?

最佳答案

我推荐你的最好方法是使用 react-native-fetch-blob,你可以像这样实现它:

const RNFetchBlob = require('react-native-fetch-blob').default;

const {
fs
} = RNFetchBlob;

const baseCacheDir = fs.dirs.CacheDir + '/videocache';

//call the downloadVideo function
downloadVideo('http://....',baseCacheDir)
//Function to download a file..
const activeDownloads = {};
function downloadVideo(fromUrl, toFile) {
// use toFile as the key
activeDownloads[toFile] = new Promise((resolve, reject) => {
RNFetchBlob
.config({path: toFile})
.fetch('GET', fromUrl)
.then(res => {
if (Math.floor(res.respInfo.status / 100) !== 2) {
throw new Error('Failed to successfully download video');
}
resolve(toFile);
})
.catch(err => {
return deleteFile(toFile)
.then(() => reject(err));
})
.finally(() => {
// cleanup
delete activeDownloads[toFile];
});
});
return activeDownloads[toFile];
}

//To delete a file..

function deleteFile(filePath) {
return fs.stat(filePath)
.then(res => res && res.type === 'file')
.then(exists => exists && fs.unlink(filePath)) //if file exist
.catch((err) => {
// swallow error to always resolve
});
}

干杯:)

关于react-native - 如何在 react-native-video 中实现缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com