choose-video.uvue 2.7 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
7
      <video class="video" :src="src" :controls="true"></video>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
8 9 10 11 12
      <view class="uni-btn-v">
        <button type="primary" @click="chooseVideo">选取视频</button>
      </view>
      <enum-data title="视频来源" :items="sourceTypeItemTypes" @change="onSourceTypeChange"></enum-data>
      <enum-data title="摄像头" :items="cameraItemTypes" @change="onCameraChange"></enum-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
13 14 15
    </view>
    <input-data title="最长拍摄时间,单位秒" defaultValue="60" type="number" @confirm="onMaxDurationConfirm"></input-data>
    <view class="uni-padding-wrap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
16 17 18 19 20 21 22 23
      <boolean-data title="是否压缩" :defaultValue="true" @change="onCompressedChange"></boolean-data>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24
  import { ItemType } from '@/components/enum-data/enum-data';
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  export default {
    data() {
      return {
        title: "chooseVideo",
        src: "",
        sourceTypeItemTypes: [{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[],
        sourceTypeItems: [["album"], ["camera"], ["album", "camera"]],
        cameraItemTypes: [{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[],
        cameraItems: ["back", "front"],
        sourceType: ["album", "camera"],
        compressed: true,
        maxDuration: 60,
        camera: "back"
      }
    },
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          sourceType: this.sourceType,
          compressed: this.compressed,
          maxDuration: this.maxDuration,
          camera: this.camera,
          // extension: ['mp4'],
          success: (res) => {
            console.log("chooseVideo success", JSON.stringify(res));
            this.src = res.tempFilePath;
          },
          fail: (err) => {
            uni.showModal({
              title: "选择视频失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          }
        })
      },
      onSourceTypeChange(value : number) {
        this.sourceType = this.sourceTypeItems[value];
      },
      onCompressedChange(value : boolean) {
        this.compressed = value;
      },
      onMaxDurationConfirm(value : number) {
        this.maxDuration = value;
      },
      onCameraChange(value : number) {
        this.camera = this.cameraItems[value];
      }
    }
  }
</script>

<style>
  .video {
    align-self: center;
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
80 81
    width: 300px;
    height: 225px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
82 83
  }
</style>