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

相关推荐
YungFan2 小时前
iOS26适配指南之UINavigationController
ios·swift
木叶丸3 小时前
Flutter 生命周期完全指南
android·flutter·ios
paopaokaka_luck4 小时前
基于SpringBoot+Vue的非遗文化传承管理系统(websocket即时通讯、协同过滤算法、支付宝沙盒支付、可分享链接、功能量非常大)
java·数据库·vue.js·spring boot·后端·spring·小程序
春哥的研究所6 小时前
AI人工智能名片小程序源码系统,名片小程序+分销商城+AI客服,包含完整搭建教程
人工智能·微信小程序·小程序
paopaokaka_luck6 小时前
智能推荐社交分享小程序(websocket即时通讯、协同过滤算法、时间衰减因子模型、热度得分算法)
数据库·vue.js·spring boot·后端·websocket·小程序
Keya7 小时前
解决 pod install 警告信息显示不完全的方法
ios·xcode·cocoapods
贝格前端工场7 小时前
小程序订阅消息设计:用户触达与隐私保护的平衡法则
大数据·小程序
weixin_lynhgworld7 小时前
盲盒一番赏小程序:用科技重新定义“未知的快乐”
科技·小程序
Bruce_Json8 小时前
微信小程序ts+sassjlin-ui
微信小程序·小程序·sass
随笔记9 小时前
uniapp开发的小程序输入框在ios自动填充密码,如何欺骗苹果手机不让自动填充
前端·ios·app