提交 75546071 编写于 作者: Q qiang

fix: 优化 uni.chooseVideo 失败返回信息

上级 ebe7c51c
......@@ -6,92 +6,80 @@ import {
invoke
} from '../../bridge'
const invokeChooseVideo = function (callbackId, type, tempFilePath = '') {
const callbackResult = {
errMsg: `chooseVideo:${type}`,
tempFilePath: tempFilePath,
duration: 0,
size: 0,
height: 0,
width: 0
}
if (type !== 'ok') {
invoke(callbackId, callbackResult)
return
}
plus.io.getVideoInfo({
filePath: tempFilePath,
success (videoInfo) {
callbackResult.size = videoInfo.size
callbackResult.duration = videoInfo.duration
callbackResult.width = videoInfo.width
callbackResult.height = videoInfo.height
invoke(callbackId, callbackResult)
},
fail () {
invoke(callbackId, callbackResult)
},
complete () {
invoke(callbackId, callbackResult)
}
})
}
const openCamera = function (callbackId, maxDuration, cameraIndex) {
const camera = plus.camera.getCamera()
camera.startVideoCapture(e => invokeChooseVideo(callbackId, 'ok', e), e => invokeChooseVideo(
callbackId, 'fail'), {
index: cameraIndex,
videoMaximumDuration: maxDuration,
filename: TEMP_PATH + '/camera/'
})
}
const openAlbum = function (callbackId) {
plus.gallery.pick(e => {
invokeChooseVideo(callbackId, 'ok', e)
}, e => invokeChooseVideo(callbackId, 'fail'), {
filter: 'video',
system: false,
filename: TEMP_PATH + '/gallery/'
})
}
export function chooseVideo ({
sourceType = ['album', 'camera'],
maxDuration = 60,
camera = 'back'
} = {}, callbackId) {
let fallback = true
const cameraIndex = (camera === 'front') ? 2 : 1
function errorCallback (error) {
error = error || {}
invoke(callbackId, {
errMsg: `chooseVideo:fail ${error.message || 'cancel'}`
})
}
function successCallback (tempFilePath = '') {
plus.io.getVideoInfo({
filePath: tempFilePath,
success (videoInfo) {
const result = {
errMsg: 'chooseVideo:ok',
tempFilePath: tempFilePath
}
result.size = videoInfo.size
result.duration = videoInfo.duration
result.width = videoInfo.width
result.height = videoInfo.height
invoke(callbackId, result)
},
errorCallback
})
}
function openAlbum () {
plus.gallery.pick(successCallback, errorCallback, {
filter: 'video',
system: false,
filename: TEMP_PATH + '/gallery/'
})
}
function openCamera () {
const plusCamera = plus.camera.getCamera()
plusCamera.startVideoCapture(successCallback, errorCallback, {
index: camera === 'front' ? 2 : 1,
videoMaximumDuration: maxDuration,
filename: TEMP_PATH + '/camera/'
})
}
if (sourceType.length === 1) {
if (sourceType[0] === 'album') {
fallback = false
openAlbum(callbackId)
openAlbum()
return
} else if (sourceType[0] === 'camera') {
fallback = false
openCamera(callbackId, maxDuration, cameraIndex)
openCamera()
return
}
}
if (fallback) {
plus.nativeUI.actionSheet({
cancel: '取消',
buttons: [{
title: '拍摄'
}, {
title: '从手机相册选择'
}]
}, e => {
switch (e.index) {
case 0:
invokeChooseVideo(callbackId, 'fail')
break
case 1:
openCamera(callbackId, maxDuration, cameraIndex)
break
case 2:
openAlbum(callbackId)
break
}
})
}
plus.nativeUI.actionSheet({
cancel: '取消',
buttons: [{
title: '拍摄'
}, {
title: '从手机相册选择'
}]
}, e => {
switch (e.index) {
case 1:
openCamera()
break
case 2:
openAlbum()
break
default:
errorCallback()
break
}
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册