需求:
- 10個視頻放入輪播圖,點擊其中一個后全屏播放
- 橫屏布局
- 判斷視頻在本地緩存是否存在,有的話讀本地,沒有的話存服務器
- 打包成ios
分析:
- 10個視頻放在同一個頁面,寫多個組件耗費性能且uniapp兼容性差,因此,拆分成兩個界面,一個界面單獨布局,一個界面單獨處理video邏輯,全屏播放,可以獲取屏幕寬高賦予給video組件,cover-view或cover-image組件添加返回首頁
- 橫屏實現代碼:
"pageOrientation": "landscape"放到page.json下的globalStyle里 - 關鍵代碼
- ios打包需要配置證書,配置完雲打包差不多等待幾分鍾既可生成下載包鏈接
關鍵代碼
//初始化video組件寬高
initResize: function() {
var that = this;
//賦予video組件寬高
uni.getSystemInfo({
success: (res) => {
//console.log(res);
that.screenHeight = res.windowHeight + "px";
that.screenWidth = res.windowWidth + "px";
}
})
},
//初始化視頻狀態
initVideoStatus(options) {
let _this = this;
//獲取本地緩存數據看是否存在
const videoHistoryAry = uni.getStorageSync('videoListAry');
console.log(videoHistoryAry);
let isExist = false;
if (videoHistoryAry != null && videoHistoryAry.length > 0) {
for (var i = 0; i < videoHistoryAry.length; i++) {
//在緩存中存在則用緩存地址,不存在則添加到新的數組中
if (videoHistoryAry[i].id == _this.id) {
console.log('緩存中存在');
isExist = true;
_this.vidsrc = videoHistoryAry[i].videoAddress;
break;
}
}
//不存在的話下載並添加新數組
if (!isExist) {
console.log(options.vidsrc);
_this.vidsrc = options.vidsrc;
const downloadItem = {
id: _this.id,
videoAddress: _this.vidsrc
}
_this.downFile('videoListAry', videoHistoryAry, downloadItem);
}
} else {
console.log("首次進入,沒有緩存過一次");
const videoHistoryAry = [];
_this.vidsrc = options.vidsrc // 我是在線鏈接
const downloadItem = {
id: _this.id,
videoAddress: _this.vidsrc
}
_this.downFile('videoListAry', videoHistoryAry, downloadItem);
}
},
// 下載視頻
downFile(setName, setSourceArr, setObj) {
console.log(setSourceArr)
const _this = this;
uni.downloadFile({
url: setObj.videoAddress,
success: (res) => {
if (res.statusCode === 200) {
console.log('下載成功地址:' + res.tempFilePath);
//保存視頻到本地
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(res) {
var savedFilePath = res.savedFilePath;
console.log('保存成功:' + savedFilePath);
setSourceArr.push({
id: setObj.id,
videoAddress: savedFilePath
});
uni.setStorageSync(setName, setSourceArr);
},
error: function(err) {
console.log(error);
}
});
}
}
});
},
鏈接引用
