choose-video.uvue 3.1 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';
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42
  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,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
43 44
        camera: "back",
        videoInfo: ""
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45 46 47 48 49 50
      }
    },
    methods: {
      chooseVideo() {
        uni.chooseVideo({
          sourceType: this.sourceType,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51
          // #ifdef APP
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
52
          compressed: this.compressed,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
53
          // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54 55 56 57 58
          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 已提交
59
            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 已提交
60 61 62 63 64 65 66 67
          },
          fail: (err) => {
            uni.showModal({
              title: "选择视频失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
68
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      },
      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 已提交
89 90
    width: 300px;
    height: 225px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
91 92
  }
</style>