首先,采用 wx.downloadFile()方法,访问视频对应的Url,回调函数返回一个该视频文件的临时路径。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210423211054145.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RvbTg3MDIyMzA1MA==,size_16,color_FFFFFF,t_70)
wx.downloadFile({
url: app.serverUrl + me.data.videoInfo.videoPath,
success: function (res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
//打印临时路径
console.log(res.tempFilePath);
}
}
})
然后采用 wx.saveVideoToPhotosAlbum()方法,保存视频到系统相册,成功!
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210423211021523.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RvbTg3MDIyMzA1MA==,size_16,color_FFFFFF,t_70)
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success:function(res) {
console.log(res.errMsg)
wx.hideLoading();
}
})
完整代码如下
//downloadFile.js
wx.showLoading({
title: '下载中...',
})
wx.downloadFile({
//视频信息的Url
url: app.serverUrl + me.data.videoInfo.videoPath,
success: function (res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
console.log(res.tempFilePath);
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success:function(res) {
console.log(res.errMsg)
wx.hideLoading();
}
})
}
}
})