su-video.vue 4.7 KB

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