video.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view class="flex justify-center align-center" :style="{ width: `${width}px` }">
  3. <video :id="`video${videoId}`" ref="`video${videoId}`" class="flex justify-center align-center"
  4. :style="{ height: `${height}px`, width: `${width}px` }" loop :src="src" :controls="false"
  5. :show-play-btn="false" :enable-progress-gesture="false" :object-fit="videoFit" @timeupdate="timeupdate"
  6. @error="playError" />
  7. <view class="position-absolute" v-if="!play && !OpenCover" @click="videoPlay">
  8. <text class="icon">&#xe60f;</text>
  9. </view>
  10. <!-- 封面图片 -->
  11. <!-- 上阴影 -->
  12. <view class="video-top" />
  13. <!-- 下阴影 -->
  14. <view class="video-bottom" />
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. videoId: {
  21. type: Number,
  22. default: 0,
  23. },
  24. src: {
  25. type: String,
  26. default: "",
  27. },
  28. play: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. height: {
  33. type: Number,
  34. default: 0,
  35. },
  36. width: {
  37. type: Number,
  38. default: 0,
  39. },
  40. objectFit: {
  41. type: String,
  42. default: "cover",
  43. },
  44. coverUrl: {
  45. //视频封面的图片
  46. type: String,
  47. default: "",
  48. },
  49. initialTime: {
  50. type: Number,
  51. default: 0,
  52. },
  53. gDuration: {
  54. type: Number,
  55. default: 999,
  56. },
  57. },
  58. data() {
  59. return {
  60. videoFit: "contain", // 优化HBuilder展示bug
  61. time: 0, // 时间类
  62. duration: 0, // 视频时长
  63. OpenCover: true, // 是否开启封面
  64. initialize: false, // 初始化
  65. videoTimer: null, // 计时器
  66. };
  67. },
  68. mounted() {
  69. this.videoFit = this.objectFit;
  70. // #ifdef APP-PLUS
  71. let sys = uni.getSystemInfoSync();
  72. if (sys.platform === "ios") {
  73. if (this.objectFit === "cover") {
  74. this.videoFit = "fill";
  75. }
  76. }
  77. // #endif
  78. let video = `video${this.videoId}`;
  79. console.log("this.videoId", this.videoId);
  80. this.videoContext = uni.createVideoContext(video, this);
  81. setTimeout(() => {
  82. this.videoContext.play();
  83. }, 10);
  84. },
  85. methods: {
  86. playError(err) {
  87. console.log("播放出错", err)
  88. },
  89. continuePlay() {
  90. console.log("继续播放")
  91. if (!this.initialize) {
  92. setTimeout(() => {
  93. this.initialize = true;
  94. this.videoPlay();
  95. }, 100);
  96. }
  97. },
  98. // 拖动滑块 1.0.9临时
  99. // changeCurrent(e) {
  100. // this.time = e.detail.value;
  101. // this.videoContext.seek(this.time);
  102. // },
  103. timeupdate(event) {
  104. // 1.0.9临时
  105. this.duration = event.detail.duration;
  106. if (this.time >= event.detail.duration) this.time = 0;
  107. this.time = event.detail.currentTime;
  108. },
  109. videoPlay() {
  110. console.log("视频播放事件", this.videoTimer);
  111. console.log("视频播放事件play", this.play);
  112. if (this.videoTimer) {
  113. clearTimeout(this.videoTimer);
  114. this.videoTimer = null;
  115. }
  116. this.videoTimer = setTimeout(() => {
  117. if (this.play) {
  118. console.log("视频播放触发");
  119. this.videoContext.play();
  120. this.OpenCover = false;
  121. } else {
  122. this.videoContext.pause();
  123. this.OpenCover = true;
  124. // 1.0.9临时
  125. // this.$emit('pause', this.time);
  126. }
  127. });
  128. // if (this.play) {
  129. // this.videoContext.play();
  130. // this.OpenCover = false;
  131. // } else {
  132. // this.videoContext.pause();
  133. // this.$emit('pause', this.time);
  134. // }
  135. },
  136. },
  137. watch: {
  138. //防抖 防止视频播放暂停太快
  139. play: function(newVal, oldVal) {
  140. console.log("newval_play", newVal);
  141. this.videoPlay();
  142. },
  143. // 1.0.9临时
  144. // startTime: {
  145. // immediate: true,
  146. // handler(newVal, oldVal) {
  147. // this.time = newVal;
  148. // }
  149. // },
  150. gDuration: {
  151. immediate: true,
  152. handler(newVal, oldVal) {
  153. this.duration = newVal;
  154. },
  155. },
  156. },
  157. computed: {
  158. barWidth() {
  159. let width = (this.time / this.duration) * parseInt(this.width);
  160. return `${width}px`;
  161. },
  162. // 1.0.9临时
  163. // startTime() {
  164. // return this.initialTime;
  165. // }
  166. },
  167. };
  168. </script>
  169. <style lang="scss" scoped>
  170. @import "@/static/css/common.css";
  171. .position-absolute {
  172. background: #000;
  173. }
  174. .icon {
  175. opacity: 0.6;
  176. font-size: 120rpx;
  177. color: #fff;
  178. }
  179. .icon-spin {
  180. //#ifndef APP-PLUS-NVUE
  181. animation: spin 1.5s infinite linear;
  182. //#endif
  183. }
  184. .video-top {
  185. position: absolute;
  186. top: 0;
  187. background-image: linear-gradient(to top,
  188. rgba(0, 0, 0, 0),
  189. rgba(0, 0, 0, 0.4));
  190. width: 750rpx;
  191. height: 300rpx;
  192. }
  193. .video-bottom {
  194. position: absolute;
  195. bottom: 0;
  196. background-image: linear-gradient(to top,
  197. rgba(0, 0, 0, 0.4),
  198. rgba(0, 0, 0, 0));
  199. width: 750rpx;
  200. height: 300rpx;
  201. }
  202. .slider-view {
  203. position: absolute;
  204. left: 0;
  205. bottom: 30px;
  206. width: 750rpx;
  207. }
  208. .progressBar {
  209. border-radius: 2rpx;
  210. height: 4rpx;
  211. background-color: rgba(255, 255, 255, 0.3);
  212. z-index: 999999;
  213. position: absolute;
  214. left: 0;
  215. bottom: 0;
  216. //#ifndef APP-PLUS-NVUE
  217. animation: flicker 4s linear infinite;
  218. animation-direction: alternate;
  219. //#endif
  220. }
  221. /* #ifndef APP-PLUS-NVUE */
  222. @keyframes flicker {
  223. 0% {
  224. box-shadow: 0 0 0 #ffffff;
  225. }
  226. /** 暂停效果 */
  227. 10% {
  228. box-shadow: 0 0 2upx #ffffff;
  229. }
  230. 50% {
  231. box-shadow: 0 0 10upx #ffffff;
  232. }
  233. 60% {
  234. box-shadow: 0 0 12upx #ffffff;
  235. }
  236. 90% {
  237. box-shadow: 0 0 18upx #ffffff;
  238. }
  239. 100% {
  240. box-shadow: 0 0 20upx #ffffff;
  241. }
  242. }
  243. @keyframes spin {
  244. 0% {
  245. -webkit-transform: rotate(0);
  246. transform: rotate(0);
  247. }
  248. 100% {
  249. -webkit-transform: rotate(359deg);
  250. transform: rotate(359deg);
  251. }
  252. }
  253. /* #endif */
  254. </style>