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

相关推荐
chaffererdog1 天前
uniapp开发微信小程序使用vk-uview-ui的uSearch搜索组件,在微信开发者工具中点击输入框会意外触发custom事件
微信小程序·小程序·uni-app
0xAaron1 天前
确定crash文件和dSYM是否对应
ios·uuid·crash·dsym
0xAaron1 天前
符号表和 dSYM UUID 确认
ios·cocoa·uuid·符号表·dsym
0xAaron1 天前
如何使用dSYM文件来符号化崩溃信息
ios·swift·调试·崩溃·符号化·dsym
2501_915918411 天前
Flutter 加固方案全解析,从 Dart 层到 IPA 成品的多工具协同防护体系
flutter·macos·ios·小程序·uni-app·cocoa·iphone
我命由我123451 天前
微信小程序 - 内容弹出框实现(Vant Weapp 实现、原生实现)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
wsxlgg1 天前
IOS 打包上传提示you do not have required contracts to perform an operation
ios
每周报刊1 天前
初代 iPhone SE 谢幕:被标为 “过时”,小屏旗舰时代彻底落幕
ios·iphone
RollingPin1 天前
iOS 动态库与静态库的区别
ios·framework·动态库·静态库·符号表·三方库·dyld
小码哥0681 天前
陪诊小程序核心功能拆解:预约、导航与提醒
小程序·项目源码·预约系统·陪诊·陪诊系统开发