choose-video.uvue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-padding-wrap">
  7. <video class="video" :src="src" :controls="true" :poster="videoCoverImage"></video>
  8. <view class="uni-title">
  9. <text class="uni-subtitle-text">视频信息</text>
  10. </view>
  11. <text>{{videoInfo}}</text>
  12. <view class="uni-btn-v">
  13. <button type="primary" @click="chooseVideo">选取视频</button>
  14. </view>
  15. <enum-data title="视频来源" :items="sourceTypeItemTypes" @change="onSourceTypeChange"></enum-data>
  16. <!-- #ifdef APP -->
  17. <enum-data title="屏幕方向" :items="orientationTypeItemTypes" @change="onOrientationTypeChange"></enum-data>
  18. <!-- #endif -->
  19. <enum-data title="摄像头" :items="cameraItemTypes" @change="onCameraChange"></enum-data>
  20. <!-- #ifdef APP-ANDROID -->
  21. <enum-data title="相册模式" :items="albumModeTypes" @change="onAlbumModeChange"></enum-data>
  22. <!-- #endif -->
  23. </view>
  24. <input-data title="最长拍摄时间,单位秒" defaultValue="60" type="number" @confirm="onMaxDurationConfirm"></input-data>
  25. <!-- #ifdef APP -->
  26. <view class="uni-padding-wrap">
  27. <boolean-data title="是否压缩(HamonyOS 不支持,推荐使用 uni.compressVideo 进行压缩)" :defaultValue="true" @change="onCompressedChange"></boolean-data>
  28. </view>
  29. <!-- #endif -->
  30. <!-- #ifdef APP -->
  31. </scroll-view>
  32. <!-- #endif -->
  33. </template>
  34. <script>
  35. import { ItemType } from '@/components/enum-data/enum-data-types';
  36. type Camera = "back" | "front"
  37. type Source = "album" | "camera"
  38. export default {
  39. data() {
  40. return {
  41. title: "chooseVideo",
  42. src: "",
  43. orientationTypeItemTypes: [{ "value": 0, "name": "竖屏" }, { "value": 1, "name": "横屏" }, { "value": 2, "name": "自动" }] as ItemType[],
  44. sourceTypeItemTypes: [{ "value": 0, "name": "从相册中选择视频" }, { "value": 1, "name": "拍摄视频" }, { "value": 2, "name": "从相册中选择视频或拍摄视频" }] as ItemType[],
  45. sourceTypeItems: [["album"], ["camera"], ["album", "camera"]] as Source[][],
  46. cameraItemTypes: [{ "value": 0, "name": "后置摄像头" }, { "value": 1, "name": "前置摄像头" }] as ItemType[],
  47. albumModeTypes: [{ "value": 0, "name": "自定义视频选择器" }, { "value": 1, "name": "系统视频选择器" }] as ItemType[],
  48. albumModeTypeItems: ["custom", "system"],
  49. cameraItems: ["back", "front"] as Camera[],
  50. sourceType: ["album", "camera"] as Source[],
  51. orientationType: "portrait",
  52. orientationTypeItems: ["portrait", "landscape", "auto"],
  53. compressed: true,
  54. maxDuration: 60,
  55. camera: "back" as Camera,
  56. videoInfo: "",
  57. videoCoverImage: "",
  58. albumMode: "custom"
  59. }
  60. },
  61. onHide() {
  62. console.log("Page Hide");
  63. },
  64. methods: {
  65. chooseVideo() {
  66. uni.chooseVideo({
  67. sourceType: this.sourceType,
  68. // #ifdef APP
  69. compressed: this.compressed,
  70. pageOrientation: this.orientationType,
  71. // #endif
  72. maxDuration: this.maxDuration,
  73. // #ifdef APP-ANDROID
  74. albumMode: this.albumMode,
  75. // #endif
  76. camera: this.camera,
  77. success: (res) => {
  78. console.log("chooseVideo success", JSON.stringify(res));
  79. this.src = res.tempFilePath;
  80. this.videoInfo = `视频长度: ${res.duration}s\n视频大小: ${Math.ceil(res.size)}KB\n视频宽度: ${res.width}\n视频高度: ${res.height}\n`;
  81. // #ifdef APP-ANDROID || APP-IOS
  82. uni.getVideoInfo({
  83. src: res.tempFilePath,
  84. success: (_res) => {
  85. if(_res.thumbTempFilePath != null) {
  86. this.videoCoverImage = _res.thumbTempFilePath!
  87. }
  88. }
  89. });
  90. // #endif
  91. },
  92. fail: (err) => {
  93. uni.showModal({
  94. title: "选择视频失败",
  95. content: JSON.stringify(err),
  96. showCancel: false
  97. });
  98. }
  99. });
  100. },
  101. onOrientationTypeChange(value : number) {
  102. this.orientationType = this.orientationTypeItems[value];
  103. },
  104. onSourceTypeChange(value : number) {
  105. this.sourceType = this.sourceTypeItems[value];
  106. },
  107. onCompressedChange(value : boolean) {
  108. this.compressed = value;
  109. },
  110. onMaxDurationConfirm(value : number) {
  111. this.maxDuration = value;
  112. },
  113. onCameraChange(value : number) {
  114. this.camera = this.cameraItems[value];
  115. },
  116. onAlbumModeChange(value : number) {
  117. this.albumMode = this.albumModeTypeItems[value]
  118. }
  119. }
  120. }
  121. </script>
  122. <style>
  123. .video {
  124. align-self: center;
  125. width: 300px;
  126. height: 225px;
  127. }
  128. </style>