su-video.vue 5.2 KB

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