video.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-common-mt">
  5. <view class="uni-list">
  6. <view class="uni-list-cell">
  7. <view class="uni-list-cell-left">
  8. <view class="uni-label">视频来源</view>
  9. </view>
  10. <view class="uni-list-cell-right">
  11. <picker :range="sourceType" @change="sourceTypeChange" :value="sourceTypeIndex">
  12. <view class="uni-input">{{sourceType[sourceTypeIndex]}}</view>
  13. </picker>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. <!-- #ifdef APP-PLUS || MP-WEIXIN -->
  19. <view class="uni-title uni-common-mt uni-common-pl">摄像头位置</view>
  20. <view class="uni-hello-text camera-tips">注意:部分 Android 手机下由于系统 ROM 不支持无法生效,打开拍摄界面后可操作切换</view>
  21. <view class="uni-list">
  22. <radio-group @change="radioChange">
  23. <label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in cameraList" :key="item.value">
  24. <radio :value="item.value" :checked="index === cameraIndex">{{item.name}}</radio>
  25. </label>
  26. </radio-group>
  27. </view>
  28. <!-- #endif -->
  29. <template v-if="!src">
  30. <view class="uni-hello-addfile" @tap="chooseVideo">+ 添加视频</view>
  31. </template>
  32. <template v-else>
  33. <video :src="src" class="video"></video>
  34. </template>
  35. </view>
  36. </template>
  37. <script>
  38. var sourceType = [
  39. ['camera'],
  40. ['album'],
  41. ['camera', 'album']
  42. ]
  43. export default {
  44. data() {
  45. return {
  46. title: 'chooseVideo',
  47. sourceTypeIndex: 2,
  48. sourceType: ['拍摄', '相册', '拍摄或相册'],
  49. src: '',
  50. cameraList: [{
  51. value: 'back',
  52. name: '后置摄像头',
  53. checked: 'true'
  54. },
  55. {
  56. value: 'front',
  57. name: '前置摄像头'
  58. },
  59. ],
  60. cameraIndex: 0
  61. }
  62. },
  63. onUnload() {
  64. this.src = '',
  65. this.sourceTypeIndex = 2,
  66. this.sourceType = ['拍摄', '相册', '拍摄或相册'];
  67. },
  68. methods: {
  69. radioChange(evt) {
  70. for (let i = 0; i < this.cameraList.length; i++) {
  71. if (this.cameraList[i].value === evt.detail.value) {
  72. this.cameraIndex = i;
  73. break;
  74. }
  75. }
  76. },
  77. sourceTypeChange: function(e) {
  78. this.sourceTypeIndex = parseInt(e.detail.value)
  79. },
  80. chooseVideo: function() {
  81. uni.chooseVideo({
  82. camera: this.cameraList[this.cameraIndex].value,
  83. sourceType: sourceType[this.sourceTypeIndex],
  84. success: (res) => {
  85. this.src = res.tempFilePath
  86. },
  87. fail: (err) => {
  88. // #ifdef MP
  89. uni.getSetting({
  90. success: (res) => {
  91. let authStatus = false;
  92. switch (this.sourceTypeIndex) {
  93. case 0:
  94. authStatus = res.authSetting['scope.camera'];
  95. break;
  96. case 1:
  97. authStatus = res.authSetting['scope.album'];
  98. break;
  99. case 2:
  100. authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
  101. break;
  102. default:
  103. break;
  104. }
  105. if (!authStatus) {
  106. uni.showModal({
  107. title: '授权失败',
  108. content: 'Hello uni-app需要从您的相机或相册获取视频,请在设置界面打开相关权限',
  109. success: (res) => {
  110. if (res.confirm) {
  111. uni.openSetting()
  112. }
  113. }
  114. })
  115. }
  116. }
  117. })
  118. // #endif
  119. }
  120. })
  121. }
  122. }
  123. }
  124. </script>
  125. <style>
  126. .video {
  127. width: 100%;
  128. }
  129. .camera-tips {
  130. padding: 10rpx 30rpx;
  131. }
  132. </style>