video.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <!-- 视频单项 -->
  3. <view class="video-layout">
  4. <video
  5. v-show="isplay"
  6. :id="`myVideo${index}`"
  7. :style="{'height':height,'width':'100%','borderRadius':`${borderRadius}rpx`}"
  8. :src="src"
  9. controls
  10. objectFit="contain"
  11. :enable-progress-gesture="true"
  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. >
  21. <!-- #ifdef APP-PLUS -->
  22. <cover-view :style="{ transform: 'translateX(' + moveX + 'px)'}" />
  23. <!-- #endif -->
  24. <!-- 不使用弹窗提示,视频内部提示可使用cover-view自定义播放提示样式 -->
  25. </video>
  26. <view
  27. v-show="!isplay"
  28. class="image"
  29. :style="{'height':height,'width':'100%','borderRadius':`${borderRadius}rpx`}"
  30. @click="beforePlay"
  31. >
  32. <image mode="aspectFill" :src="poster" />
  33. <view class="play" />
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. props: {
  40. moveX: { type: [Number, String], default: 0 }, // 轮播图兼容滑动兼容-单独使用可不传
  41. index: { type: [Number, String], default: 0 }, // 下标索引
  42. height: { type: String, default: '420rpx' }, // 视频高度
  43. borderRadius: { type: Number, default: 0 }, // 圆角值,单位rpx
  44. initialTime: { type: Number, default: 0 }, // 指定视频初始播放位置,单位为秒(s)
  45. videoSize: { type: [Number, String], default: 10 }, // 视频大小
  46. ignoreTip: { type: Boolean, default: true }, // 播放环境提示
  47. src: { // 播放地址
  48. type: String,
  49. default: 'https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/360e4b20-4f4b-11eb-8a36-ebb87efcf8c0.mp4'
  50. },
  51. poster: { // 封面
  52. type: String,
  53. default: 'https://img1.baidu.com/it/u=1601695551,235775011&fm=26&fmt=auto'
  54. }
  55. },
  56. data() {
  57. return {
  58. // videoContext: null, // 视频实例
  59. isplay: false, // 播放状态
  60. isTip: true// 是否提示
  61. }
  62. },
  63. watch: {
  64. ignoreTip: {
  65. handler(v) {
  66. this.isTip = v
  67. },
  68. immediate: true
  69. }
  70. },
  71. onReady() {
  72. // this.videoContext = uni.createVideoContext('myVideo')
  73. },
  74. methods: {
  75. timeupdate(e) { // 播放进度变化时触发--更新播放缓存
  76. this.$emit('timeupdate', e)
  77. },
  78. videoErrorCallback: function(e) {
  79. console.log('视频错误信息:')
  80. console.log(e.target.errMsg)
  81. },
  82. play() {
  83. },
  84. pause() {
  85. console.log('暂停')
  86. this.isplay = false
  87. },
  88. startPlay() { // 开始播放
  89. this.isplay = true
  90. this.$nextTick(() => {
  91. const id = `myVideo${this.index}`
  92. const video = uni.createVideoContext(id)
  93. video.play()
  94. })
  95. },
  96. pausePlay(){//暂停播放
  97. const id = `myVideo${this.index}`
  98. const video = uni.createVideoContext(id)
  99. video.pause()
  100. this.isplay = false
  101. },
  102. beforePlay() { // 播放前拦截
  103. this.isplay = true // 拦截前显示播放视频
  104. if (!this.isTip) return this.startPlay() // 不提示直接播放
  105. // https://uniapp.dcloud.io/api/system/network?id=getnetworktype
  106. uni.getNetworkType({
  107. success: (res) => {
  108. const networkType = res.networkType
  109. if (networkType === 'wifi' || networkType === 'ethernet') {
  110. this.startPlay()
  111. } else {
  112. uni.showModal({
  113. title: '提示',
  114. content: `当前为移动网络,播放视频需消耗${this.videoSize}M流量,是否继续播放?`,
  115. success: (res) => {
  116. if (res.confirm) {
  117. this.startPlay()
  118. this.isTip = false
  119. } else {
  120. this.isplay = false
  121. }
  122. }
  123. })
  124. }
  125. }
  126. })
  127. }
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. .video-layout{
  133. display: flex;
  134. align-items: center;
  135. // video{
  136. // width: 100%;
  137. // height: 100%;
  138. // /deep/.uni-video-container{
  139. // width: auto;
  140. // height: auto;
  141. // }
  142. // }
  143. .image{
  144. position: relative;
  145. width: 100%;
  146. height: 100%;
  147. /deep/uni-image{
  148. width: 100%;
  149. height: 100%;
  150. }
  151. .play{
  152. position: absolute;
  153. left: 50%;
  154. top: 43%;
  155. width: 80rpx;
  156. height: 80rpx;
  157. transform: translate(-50%,-50%);
  158. background-image: url('@/static/play.png');
  159. background-size: contain;
  160. background-repeat: no-repeat;
  161. background-color: rgba($color: #000000, $alpha: 0.1);
  162. border-radius: 50%;
  163. }
  164. }
  165. }
  166. </style>