su-video.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="ui-video-wrap">
  3. <video
  4. :id="`sVideo${uid}`"
  5. class="radius"
  6. :style="[{ height: height + 'rpx' }]"
  7. :src="src"
  8. controls
  9. object-fit="contain"
  10. :enable-progress-gesture="state.enableProgressGesture"
  11. :initial-time="initialTime"
  12. x5-video-player-type="h5"
  13. x-webkit-airplay="allow"
  14. webkit-playsinline="true"
  15. @error="videoErrorCallback"
  16. @timeupdate="timeupdate"
  17. @play="play"
  18. @pause="pause"
  19. @ended="end"
  20. :poster="poster"
  21. >
  22. <!-- #ifdef APP-PLUS -->
  23. <cover-view :style="{ transform: 'translateX(' + moveX + 'px)' }" />
  24. <!-- #endif -->
  25. </video>
  26. <!-- <view
  27. v-show="!state.isplay"
  28. class="poster-wrap radius"
  29. :style="{ height: height + 'px' }"
  30. @click="beforePlay"
  31. >
  32. <image class="poster-image" mode="aspectFill" :src="poster" v-if="poster" />
  33. <view class="play-icon ss-flex ss-row-center ss-col-center">
  34. <text class="cicon-play-arrow ss-font-40"></text>
  35. </view>
  36. </view> -->
  37. </view>
  38. </template>
  39. <script setup>
  40. /**
  41. * 视频组件
  42. *
  43. * @property {Number} uid = 0 - 当前轮播下标,还用来标记视频Id
  44. * @property {Number} moveX = 0 - app端轮播滑动距离
  45. * @property {String} height = 300 - 高度(rpx)
  46. * @property {String} width = 750 - 宽度(rpx)
  47. * @property {Number} initialTime = 0 - 指定视频播放位置
  48. * @property {String} videoSize - 视频大小
  49. * @property {String} src - 视频播放地址
  50. * @property {String} poster - 视频封面
  51. *
  52. *
  53. */
  54. import { reactive, nextTick, getCurrentInstance } from 'vue';
  55. import sheep from '@/sheep';
  56. const vm = getCurrentInstance();
  57. // 数据
  58. const state = reactive({
  59. // #ifdef APP-PLUS
  60. enableProgressGesture: true, // 手势滑动
  61. // #endif
  62. // #ifndef APP-PLUS
  63. enableProgressGesture: false, // 手势滑动
  64. // #endif
  65. showModal: false, // 弹框
  66. });
  67. // 接收参数
  68. const props = defineProps({
  69. moveX: {
  70. type: [Number],
  71. default: 0,
  72. },
  73. // 下标索引
  74. uid: {
  75. type: [Number, String],
  76. default: 0,
  77. },
  78. // 视频高度
  79. height: {
  80. type: Number,
  81. default: 300,
  82. },
  83. // 视频宽度
  84. width: {
  85. type: Number,
  86. default: 750,
  87. },
  88. // 指定视频初始播放位置,单位为秒(s)
  89. initialTime: {
  90. type: Number,
  91. default: 0,
  92. },
  93. src: {
  94. type: String,
  95. default: '',
  96. },
  97. poster: {
  98. type: String,
  99. default: 'https://img1.baidu.com/it/u=1601695551,235775011&fm=26&fmt=auto',
  100. },
  101. });
  102. // 事件
  103. const emits = defineEmits(['videoTimeupdate']);
  104. // 播放进度变化时触发,播放进度传给父组件
  105. const timeupdate = (e) => {
  106. emits('videoTimeupdate', e);
  107. };
  108. const videoErrorCallback = (e) => {
  109. console.log('视频错误信息:', e.target.errMsg);
  110. };
  111. // 当开始/继续播放时触发play事件
  112. const play = () => {
  113. console.log('视频开始');
  114. };
  115. // 当暂停播放时触发 pause 事件
  116. const pause = () => {
  117. console.log('视频暂停');
  118. };
  119. // 视频结束触发end 时间
  120. const end = () => {
  121. console.log('视频结束');
  122. };
  123. // 开始播放
  124. const startPlay = () => {
  125. nextTick(() => {
  126. const video = uni.createVideoContext(`sVideo${props.index}`, vm);
  127. video.play();
  128. });
  129. };
  130. //暂停播放
  131. const pausePlay = () => {
  132. const video = uni.createVideoContext(`sVideo${props.index}`, vm);
  133. video.pause();
  134. };
  135. // 播放前拦截
  136. const beforePlay = () => {
  137. uni.getNetworkType({
  138. success: (res) => {
  139. console.log(res.networkType, 'res.networkType');
  140. const networkType = res.networkType;
  141. // if (networkType === 'wifi' || networkType === 'ethernet') {
  142. // startPlay();
  143. // } else {
  144. // uni.showModal({
  145. // title: '提示',
  146. // content: `当前为移动网络,播放视频需消耗手机流量,是否继续播放?${networkType}`,
  147. // success: (res) => {
  148. // if (res.confirm) {
  149. // startPlay();
  150. // } else {
  151. // state.isplay = false;
  152. // }
  153. // },
  154. // });
  155. // sheep.$helper.toast('正在消耗流量播放');
  156. // startPlay();
  157. // }
  158. startPlay();
  159. },
  160. });
  161. };
  162. // 抛出方法供父组件调用
  163. defineExpose({
  164. pausePlay,
  165. });
  166. </script>
  167. <style lang="scss" scoped>
  168. .radius {
  169. width: 100%;
  170. }
  171. .ui-video-wrap {
  172. display: flex;
  173. align-items: center;
  174. justify-content: center;
  175. .poster-wrap {
  176. position: relative;
  177. width: 100%;
  178. height: 100%;
  179. .poster-image {
  180. width: 100%;
  181. height: 100%;
  182. }
  183. .play-icon {
  184. position: absolute;
  185. left: 50%;
  186. top: 50%;
  187. width: 80rpx;
  188. height: 80rpx;
  189. transform: translate(-50%, -50%);
  190. background-color: rgba($color: #000000, $alpha: 0.1);
  191. border-radius: 50%;
  192. }
  193. }
  194. }
  195. </style>