uniapp解决app端视频最长60秒问题(支持安卓,鸿蒙,IOS没测试)

遇到这个问题时我去百度搜了很多答案,基本都是通过调用安卓原生android.provider.MediaStore来解决这个问题,但是在测试的过程中发现了两个问题

1:貌似该api是调用的安卓系统底层未进行优化的摄像功能,而不是我们手机上可以看到的经过优化的相机软件(纯猜测,反正我是没有通过该api实现自定义他的后缀名,分辨率等功能),并且产生的视频很大

2:发现在鸿蒙的测试机中,有时会出现调起摄像头黑屏的情况

基于这两点又疯狂百度,最后偶然在uniapp的官网的uni.chooseImage(OBJECT)这里发现了plus.camera 通过h5+api 解决所有问题,并进行压缩减小体积,ios并没有测试

附代码:

function android (options) {
  const crm = plus.camera.getCamera()
  crm.startVideoCapture((filePath) => {
    plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
      entry.file(function (file) {
        uni.showLoading({
          title: '加载中...',
          mask:true
        });
        uni.compressVideo({
          src: entry.fullPath,
          quality: 'low',
          success: (res) => { // 压缩成功
            file.size = res.size;
            uni.hideLoading();
            typeof options.success === "function" &&
            options.success({
              fullPath: res.tempFilePath,
              file,
            })
          },
          fail: (err) => {
            typeof options.success === "function" &&
              options.success({
                fullPath: entry.fullPath,
                file,
            })
            uni.hideLoading();
          }
        });
      })
    })
  }, (err) => {
    typeof options.fail === "function" && options.fail(err)
  }, {
    optimize: true,
    resolution: crm.supportedVideoResolutions[crm.supportedVideoResolutions.length - 1],
    format: 'mp4',
    videoMaximumDuration: 300,
    filename: `_doc/uniapp_temp_${Date.now()}/download/`
  })
}

function ios(options) {
  let cmr = plus.camera.getCamera();
  let res = cmr.supportedVideoResolutions[0];
  let fmt = cmr.supportedVideoFormats[0];
  cmr.startVideoCapture(
    function (path) {
      plus.io.resolveLocalFileSystemURL(
        path,
        (entry) => {
          console.log(entry);
          entry.file(function (file) {
            console.log(entry.fullPath);
            typeof options.success === "function" &&
              options.success({
                fullPath: entry.fullPath,
                file,
              });
          });
        },
        (e) => {}
      );
    },
    function (error) {
      typeof options.fail === "function" && options.fail(error);
    },
    {
      resolution: res,
      format: fmt,
      videoMaximumDuration: options.time || 10,
    }
  );
}
export default {
  /**
   *  options.time [number] 视频最大长度 单位s
   *  options.success [function] 成功回调函数
   *  options.fail [function] 失败回调函数
   */
  camera(options) {
    if (uni.getSystemInfoSync().platform === "android") {
      console.log("安卓");
      android(options);
    } else {
      ios(options);
      console.log("ios");
    }
  },
};

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值