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