choose-video.js 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  TEMP_PATH
} from '../constants'

import {
  invoke
} from '../../bridge'

9
import {
Q
qiang 已提交
10 11
  warpPlusErrorCallback,
  getFileName
12 13
} from '../util'

14 15 16 17
import {
  t
} from 'uni-core/helpers/i18n'

fxy060608's avatar
fxy060608 已提交
18
export function chooseVideo ({
Q
qiang 已提交
19
  sourceType,
Q
qiang 已提交
20
  compressed,
Q
qiang 已提交
21 22
  maxDuration,
  camera
fxy060608's avatar
fxy060608 已提交
23
} = {}, callbackId) {
24
  const errorCallback = warpPlusErrorCallback(callbackId, 'chooseVideo', 'cancel')
25 26

  function successCallback (tempFilePath = '') {
27
    const filename = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}`
28 29 30
    const compressVideo = compressed ? new Promise((resolve) => {
      plus.zip.compressVideo({
        src: tempFilePath,
31
        filename
32 33 34 35 36
      }, ({ tempFilePath }) => {
        resolve(tempFilePath)
      }, () => {
        resolve(tempFilePath)
      })
37
    }) : Promise.resolve(tempFilePath)
Q
qiang 已提交
38 39 40
    if (compressed) {
      plus.nativeUI.showWaiting()
    }
41
    compressVideo.then(tempFilePath => {
Q
qiang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      if (compressed) {
        plus.nativeUI.closeWaiting()
      }
      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)
57
        },
58
        fail: errorCallback
Q
qiang 已提交
59
      })
60 61 62 63
    })
  }

  function openAlbum () {
64
    plus.gallery.pick(({ files }) => successCallback(files[0]), errorCallback, {
65 66
      filter: 'video',
      system: false,
67 68 69
      // 不启用 multiple 时 system 无效
      multiple: true,
      maximum: 1,
70 71
      filename: TEMP_PATH + '/gallery/',
      permissionAlert: true
72 73 74 75 76 77 78 79 80 81 82 83
    })
  }

  function openCamera () {
    const plusCamera = plus.camera.getCamera()
    plusCamera.startVideoCapture(successCallback, errorCallback, {
      index: camera === 'front' ? 2 : 1,
      videoMaximumDuration: maxDuration,
      filename: TEMP_PATH + '/camera/'
    })
  }

fxy060608's avatar
fxy060608 已提交
84
  if (sourceType.length === 1) {
Q
qiang 已提交
85
    if (sourceType.includes('album')) {
86 87
      openAlbum()
      return
Q
qiang 已提交
88
    } else if (sourceType.includes('camera')) {
89 90
      openCamera()
      return
fxy060608's avatar
fxy060608 已提交
91 92
    }
  }
93
  plus.nativeUI.actionSheet({
94
    cancel: t('uni.chooseVideo.cancel'),
95
    buttons: [{
96
      title: t('uni.chooseVideo.sourceType.camera')
97
    }, {
98
      title: t('uni.chooseVideo.sourceType.album')
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }]
  }, e => {
    switch (e.index) {
      case 1:
        openCamera()
        break
      case 2:
        openAlbum()
        break
      default:
        errorCallback()
        break
    }
  })
fxy060608's avatar
fxy060608 已提交
113
}