uni-app 做安卓广告,项目需求根据设备id展示不同的广告内容;
vedio文件
<!-- 视频文件 -->
<video id="myVideo" class="video" :src="video_url" object-fit="fill" autoplay loop show-center-play-btn="playBtn" show-loading="loading" @error="videoErrorCallback" controls="false"></video>
第一、先做了本地配置文件config.json数据读取, 将文件放到项目文件目录:Android/data/uni.UNIEA9241B/apps/_UNI_EA9241B/doc/config.json;
//*** 读取本地文件
fileReader () {
// 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量
plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function(fs){
// fs.root是根目录操作对象DirectoryEntry
fs.root.getFile('config.json', {create: true}, function(fileEntry){
fileEntry.file( function(file){
var fileReader = new plus.io.FileReader();
fileReader.readAsText(file, 'utf-8');
fileReader.onloadend = function(e) {
var result = new Function("return" + e.target.result)();//转换后的JSON对象
// console.log(result)
that.eid = result.eid;
}
});
});
})
},
获取到配置的设备id,在进行接口请求;
第二、将设备视频版本存到本地(节省流量),根据视频版本不同,进行视频下载;
if(uni.getStorageSync('versions') == res.data.video_list[0].versions){
that.url = uni.getStorageSync('url');
}else{
//将资源版本存到缓存
uni.setStorageSync('versions', res.data.video_list[0].versions);
that.deleteFile(url);
}
第三、视频可以下载,但是是根据时间戳自动往下存储的,为了节省内存,先做本地删除在做下载处理;
//*** 先删除本地文件
deleteFile(url) {
uni.getSavedFileList({
success:(res)=> {
if (res.fileList.length > 0) {
uni.removeSavedFile({
filePath: res.fileList[0].filePath,
complete: function(res) {
that.downloadFile(url)
}
});
} else{
that.downloadFile(url)
}
}
});
},
//*** 再下载文件
downloadFile(url){
uni.downloadFile({
url: url, //下载地址接口返回
success: (data) => {
if (data.statusCode === 200) {
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath, //临时路径
success: function(res) {
that.url = res.savedFilePath;
uni.setStorageSync('url', res.savedFilePath);
}
});
}
},
fail: (err) => {
uni.showToast({
icon: 'none',
mask: true,
title: '失败请重新下载',
});
},
});
},
视频保存在:Android/data/uni.UNIEA9241B/apps/_UNI_EA9241B/doc/uniapp_save/1602736024662.mp4
这种逻辑可以实现将视频文件保存在本地并播放,但是不能重命名文件,也不能指定文件的保存路径;