解决 iOS 端小程序「saveVideoToPhotosAlbum:fail invalid video」问题

场景复现:

js 复制代码
const url = 'https://mobvoi-digitalhuman-video-public.weta365.com/1788148372310446080.mp4'

uni.downloadFile({
  url,
  success: (res) => {
    uni.saveVideoToPhotosAlbum({
      filePath: res.tempFilePath,
      success: (res) => {
        console.log("res => ", res);
        uni.showToast({
          title: '下载成功',
          icon: 'success'
        });
      },
      fail(err) {
        console.log("err => ", err);
        uni.showToast({
          title: '保存失败',
          icon: 'error'
        });
      },
    });
  },
  fail(err) {
    uni.showToast({
      title: '下载失败,请稍后再试',
      icon: 'error'
    });
  },
});

PC 和安卓没啥事,但是 iOS 上就会有这么个玩意出来:

js 复制代码
{
    errMsg: "saveVideoToPhotosAlbum:fail invalid video"
}

这个讨论中的有效信息可能是最多的,可那位说使用 compressVideo() 的老哥也没贴个实例。也尝试去用了,但是又碰到了另一个错误,真是世间 Bug 与你环环相扣=_=

于是换了个思路,我去翻了一下 Uni 插件市场,看到了这个:「移动端-H5-小程序下载保存图片,文档和视频到手机」

它不是能成功下载吗,那我就去借鉴一下它的源码,看看它是怎么实现的。

这个插件的源码就不说了,有兴趣自己去看,我就贴一下最终成功下载的代码:

js 复制代码
const url = 'https://mobvoi-digitalhuman-video-public.weta365.com/1788148372310446080.mp4';

const fileName = new Date().valueOf();

//  拼接本地文件路径
const filePath = `${uni.env.USER_DATA_PATH}/${fileName}.${url.split('.').pop()}`;

uni.downloadFile({
  url,
  filePath,
  success: (res) => {
    uni.saveVideoToPhotosAlbum({
      filePath: res.filePath,
      success: (res) => {
        console.log('res => ', res);
        uni.showToast({
          title: '下载成功',
          icon: 'success'
        });
        const fileMgr = uni.getFileSystemManager();
        // 删除本地文件
        fileMgr.unlink({
          filePath,
          success(r) {
            console.log('unlink-getFileSystemManager');
            console.log(r);
          }
        });
      },
      fail(err) {
        console.log('err => ', err);
        uni.showToast({
          title: '保存失败',
          icon: 'error'
        });
      }
    });
  },
  fail(err) {
    uni.showToast({
      title: '下载失败,请稍后再试',
      icon: 'error'
    });
  }
});

-END-

相关推荐
毕设源码-郭学长22 分钟前
【开题答辩全过程】以 酒店预约微信小程序为例,包含答辩的问题和答案
微信小程序·小程序
week_泽2 小时前
小程序云数据库查询操作_2
数据库·小程序
说私域3 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的线上线下流量转化运营策略研究
人工智能·小程序·开源
项目題供诗3 小时前
微信小程序黑马优购(项目)(十三)
微信小程序·小程序
毛发浓密的女猴子3 小时前
SSE Connect 数据解析详解
ios
万岳科技系统开发4 小时前
开源知识付费源码:实现在线课程系统与会员管理
开发语言·小程序
青青子衿越4 小时前
微信小程序垂直滚动效果显示通知消息(无限循环滚动)
微信小程序·小程序
说私域4 小时前
从“打杂”到“战略中枢”:开源AI大模型、AI智能名片与S2B2C商城小程序重构运营价值体系
人工智能·小程序·重构
烟囱土著4 小时前
捣鼓15天,我写了一个拼音发音点读小程序
学习·微信·微信小程序·小程序·拼音