su-video.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. console.log('视频错误信息:', e.target.errMsg);
  105. };
  106. // 当开始/继续播放时触发play事件
  107. const play = () => {
  108. console.log('视频开始');
  109. };
  110. // 当暂停播放时触发 pause 事件
  111. const pause = () => {
  112. console.log('视频暂停');
  113. };
  114. // 视频结束触发end 时间
  115. const end = () => {
  116. console.log('视频结束');
  117. };
  118. // 开始播放
  119. const startPlay = () => {
  120. nextTick(() => {
  121. const video = uni.createVideoContext(`sVideo${props.index}`, vm);
  122. video.play();
  123. });
  124. };
  125. //暂停播放
  126. const pausePlay = () => {
  127. const video = uni.createVideoContext(`sVideo${props.index}`, vm);
  128. video.pause();
  129. };
  130. // 播放前拦截
  131. const beforePlay = () => {
  132. uni.getNetworkType({
  133. success: (res) => {
  134. const networkType = res.networkType;
  135. // if (networkType === 'wifi' || networkType === 'ethernet') {
  136. // startPlay();
  137. // } else {
  138. // uni.showModal({
  139. // title: '提示',
  140. // content: `当前为移动网络,播放视频需消耗手机流量,是否继续播放?${networkType}`,
  141. // success: (res) => {
  142. // if (res.confirm) {
  143. // startPlay();
  144. // } else {
  145. // state.isplay = false;
  146. // }
  147. // },
  148. // });
  149. // sheep.$helper.toast('正在消耗流量播放');
  150. // startPlay();
  151. // }
  152. startPlay();
  153. },
  154. });
  155. };
  156. // 抛出方法供父组件调用
  157. defineExpose({
  158. pausePlay,
  159. });
  160. </script>
  161. <style lang="scss" scoped>
  162. .radius {
  163. width: 100%;
  164. }
  165. .ui-video-wrap {
  166. display: flex;
  167. align-items: center;
  168. justify-content: center;
  169. .poster-wrap {
  170. position: relative;
  171. width: 100%;
  172. height: 100%;
  173. .poster-image {
  174. width: 100%;
  175. height: 100%;
  176. }
  177. .play-icon {
  178. position: absolute;
  179. left: 50%;
  180. top: 50%;
  181. width: 80rpx;
  182. height: 80rpx;
  183. transform: translate(-50%, -50%);
  184. background-color: rgba($color: #000000, $alpha: 0.1);
  185. border-radius: 50%;
  186. }
  187. }
  188. }
  189. </style>