su-video.vue 5.5 KB

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