choose-video.uvue 3.2 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
      <view class="uni-title">
        <text class="uni-subtitle-text">视频信息</text>
      </view>
      <text>{{videoInfo}}</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
12 13 14 15 16
      <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 已提交
17 18
    </view>
    <input-data title="最长拍摄时间,单位秒" defaultValue="60" type="number" @confirm="onMaxDurationConfirm"></input-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
19
    <!-- #ifdef APP -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
20
    <view class="uni-padding-wrap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
21 22
      <boolean-data title="是否压缩" :defaultValue="true" @change="onCompressedChange"></boolean-data>
    </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23
    <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24 25 26 27 28 29
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30
  import { ItemType } from '@/components/enum-data/enum-data';
雪洛's avatar
雪洛 已提交
31 32
  type Camera = "back" | "front"
  type Source = "album" | "camera"
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33 34 35 36 37 38
  export default {
    data() {
      return {
        title: "chooseVideo",
        src: "",
        sourceTypeItemTypes: [{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[],
雪洛's avatar
雪洛 已提交
39
        sourceTypeItems: [["album"], ["camera"], ["album", "camera"]] as Source[][],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
40
        cameraItemTypes: [{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[],
雪洛's avatar
雪洛 已提交
41 42
        cameraItems: ["back", "front"] as Camera[],
        sourceType: ["album", "camera"] as Source[],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
43 44
        compressed: true,
        maxDuration: 60,
雪洛's avatar
雪洛 已提交
45
        camera: "back" as Camera,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46
        videoInfo: ""
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
47 48 49 50 51 52
      }
    },
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          sourceType: this.sourceType,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53
          // #ifdef APP
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54
          compressed: this.compressed,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
55
          // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
56 57 58 59 60
          maxDuration: this.maxDuration,
          camera: this.camera,
          success: (res) => {
            console.log("chooseVideo success", JSON.stringify(res));
            this.src = res.tempFilePath;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61
            this.videoInfo = `视频长度: ${res.duration}s\n视频大小: ${Math.ceil(res.size / 1024)}KB\n视频宽度: ${res.width}\n视频高度: ${res.height}\n`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
62 63 64 65 66 67 68 69
          },
          fail: (err) => {
            uni.showModal({
              title: "选择视频失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
70
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      },
      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 已提交
91 92
    width: 300px;
    height: 225px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
93 94
  }
</style>