解决 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-

相关推荐
2501_915921431 天前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915921431 天前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
@二十六1 天前
微信小程序订阅消息工具封装,兼容一次性订阅和长期订阅
微信小程序·小程序·订阅消息
dcloud_jibinbin1 天前
【uniapp】体验优化:开源工具集 uni-toolkit 发布
前端·webpack·性能优化·小程序·uni-app·vue
用力的活着1 天前
uniapp 微信小程序蓝牙接收中文乱码
微信小程序·小程序·uni-app
流***陌1 天前
陪诊陪护小程序前端功能解析:就医照料的便捷对接与全流程保障
小程序
说私域1 天前
开源AI智能名片链动2+1模式S2B2C商城小程序在个性化与小众化消费崛起中的营销宣传策略研究
人工智能·小程序
知识分享小能手1 天前
微信小程序入门学习教程,从入门到精通,电影之家小程序项目知识点详解 (17)
前端·javascript·学习·微信小程序·小程序·前端框架·vue
猪哥帅过吴彦祖1 天前
Flutter 系列教程:列表与网格 - `ListView` 和 `GridView`
前端·flutter·ios
00后程序员张1 天前
Fiddler抓包工具使用教程,代理设置与调试方法实战解析(含配置技巧)
前端·测试工具·ios·小程序·fiddler·uni-app·webview