遇到这个问题时我去百度搜了很多答案,基本都是通过调用安卓原生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");
}
},
};