choose-video.uvue 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <video class="video" :src="src" :controls="true"></video>
      <view class="uni-title">
        <text class="uni-subtitle-text">视频信息</text>
      </view>
      <text>{{videoInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseVideo">选取视频</button>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
15
      <enum-data title="视频来源" :items="sourceTypeItemTypes" @change="onSourceTypeChange"></enum-data>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      <enum-data title="屏幕方向" :items="orientationTypeItemTypes" @change="onOrientationTypeChange"></enum-data>
      <enum-data title="摄像头" :items="cameraItemTypes" @change="onCameraChange"></enum-data>
    </view>
    <input-data title="最长拍摄时间,单位秒" defaultValue="60" type="number" @confirm="onMaxDurationConfirm"></input-data>
    <!-- #ifdef APP -->
    <view class="uni-padding-wrap">
      <boolean-data title="是否压缩" :defaultValue="true" @change="onCompressedChange"></boolean-data>
    </view>
    <!-- #endif -->
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  import { ItemType } from '@/components/enum-data/enum-data-types';
  type Camera = "back" | "front"
  type Source = "album" | "camera"
  export default {
    data() {
      return {
        title: "chooseVideo",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38
        src: "",
39 40 41 42 43
        orientationTypeItemTypes: [{ "value": 0, "name": "竖屏" }, { "value": 1, "name": "横屏" }, { "value": 2, "name": "自动" }] as ItemType[],
        sourceTypeItemTypes: [{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[],
        sourceTypeItems: [["album"], ["camera"], ["album", "camera"]] as Source[][],
        cameraItemTypes: [{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[],
        cameraItems: ["back", "front"] as Camera[],
雪洛's avatar
雪洛 已提交
44
        sourceType: ["album", "camera"] as Source[],
45 46 47 48 49 50 51 52 53 54 55 56 57
        orientationType: "portrait",
        orientationTypeItems: ["portrait", "landscape", "auto"],
        compressed: true,
        maxDuration: 60,
        camera: "back" as Camera,
        videoInfo: ""
      }
    },
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          sourceType: this.sourceType,
          // #ifdef APP
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
58
          compressed: this.compressed,
59 60
          pageOrientation: this.orientationType,
          // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61
          maxDuration: this.maxDuration,
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

          camera: this.camera,
          success: (res) => {
            console.log("chooseVideo success", JSON.stringify(res));
            this.src = res.tempFilePath;
            this.videoInfo = `视频长度: ${res.duration}s\n视频大小: ${Math.ceil(res.size / 1024)}KB\n视频宽度: ${res.width}\n视频高度: ${res.height}\n`;
          },
          fail: (err) => {
            uni.showModal({
              title: "选择视频失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          }
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
77
      },
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      onOrientationTypeChange(value : number) {
        this.orientationType = this.orientationTypeItems[value];
      },
      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;
    width: 300px;
    height: 225px;
  }
</style>