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

相关推荐
Zender Han1 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios
如此风景3 小时前
IOS开发SwiftUI相关学习记录
ios
2501_915909063 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview
Redundantº9 小时前
Uniapp 适配安卓与 iOS 的 PDF、DOC 文件上传
android·ios·pdf·uni-app·webview
毕设源码-钟学长10 小时前
【开题答辩全过程】以 基于微信小程序的记账系统为例,包含答辩的问题和答案
微信小程序·小程序
sheji341610 小时前
【开题答辩全过程】以 基于微信小程序的会议预定系统设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
lancoff11 小时前
#5 ScrollViewReader
ios·swiftui
lancoff11 小时前
#6 GeometryReader
ios·swiftui
计算机毕设指导611 小时前
基于微信小程序的电子数据取证知识测试系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij idea
毕设源码-钟学长11 小时前
【开题答辩全过程】以 基于微信小程序的汉服配饰交流平台为例,包含答辩的问题和答案
微信小程序·小程序