'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)
    }
  })
}