提交 8c2eee57 编写于 作者: Q qiang

feat(h5): uni.getVideoInfo

上级 20e05c16
......@@ -9,9 +9,10 @@ const files = {}
/**
* 从url读取File
* @param {string} url
* @param {boolean} local
* @param {Promise}
*/
export function urlToFile (url) {
export function urlToFile (url, local) {
var file = files[url]
if (file) {
return Promise.resolve(file)
......@@ -19,6 +20,9 @@ export function urlToFile (url) {
if (/^data:[a-z-]+\/[a-z-]+;base64,/.test(url)) {
return Promise.resolve(base64ToFile(url))
}
if (local) {
return Promise.reject(new Error('not find'))
}
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
......
import {
urlToFile
} from 'uni-platform/helpers/file'
export function getVideoInfo ({
src
}, callbackId) {
const {
invokeCallbackHandler: invoke
} = UniServiceJSBridge
urlToFile(src, true).then(file => {
return file
}).catch(() => {
return {}
}).then(file => {
const result = file.size ? {
size: file.size,
errMsg: 'getVideoInfo:ok'
} : {
errMsg: 'getVideoInfo:fail'
}
const video = document.createElement('video')
if (video.onloadedmetadata !== undefined) {
// 部分浏览器(如微信内置浏览器)未播放无法触发loadedmetadata事件
const handle = setTimeout(() => {
video.onloadedmetadata = null
video.onerror = null
invoke(callbackId, result)
}, src.startsWith('data:') || src.startsWith('blob:') ? 300 : 3000)
// 尝试获取视频的宽高信息
video.onloadedmetadata = function () {
clearTimeout(handle)
video.onerror = null
invoke(callbackId, Object.assign(result, {
size: file.size,
duration: video.duration || 0,
width: video.videoWidth || 0,
height: video.videoHeight || 0,
errMsg: 'getVideoInfo:ok'
}))
}
video.onerror = function () {
clearTimeout(handle)
video.onloadedmetadata = null
invoke(callbackId, result)
}
video.src = src
} else {
invoke(callbackId, result)
}
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册