FullProgress.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <script>
  2. export default {
  3. name: "FullProgress",
  4. props: {
  5. playerInstance: {
  6. type: Object,
  7. default: () => {
  8. return {}
  9. }
  10. }
  11. },
  12. data() {
  13. return {
  14. isDrag: false,
  15. // 总时长
  16. totalTime: 0,
  17. // 当前播放时间
  18. currentTime: 0,
  19. // 拖拽进度
  20. progress: 0,
  21. // 拖拽开始位置
  22. startX: 0,
  23. // 拖拽开始时的进度
  24. startProgress: 0,
  25. // 定时器
  26. timer: null,
  27. // 是否在播放
  28. isPlay: false,
  29. }
  30. },
  31. mounted() {
  32. this.$nextTick(() => {
  33. this.registerEvent();
  34. })
  35. },
  36. methods: {
  37. // 注册监听事件
  38. registerEvent() {
  39. // 初始化完成
  40. this.playerInstance.on("player_init_over", (totalTime) => {
  41. this.totalTime = totalTime || 0;
  42. })
  43. // 播放
  44. this.playerInstance.on('player_play', (currentTime) => {
  45. this.currentTime = currentTime;
  46. if(currentTime === 0) this.progress = 0;
  47. this.startTimer();
  48. this.isPlay = true;
  49. });
  50. // 暂停
  51. this.playerInstance.on('player_pause', () => {
  52. this.pauseTimer();
  53. this.isPlay = false;
  54. });
  55. // 拖拽结束
  56. this.playerInstance.on("player_drag_seek", (currentTime) => {
  57. this.currentTime = currentTime;
  58. // 计算当时的进度
  59. this.progress = (currentTime / this.totalTime) * 100;
  60. })
  61. },
  62. handlePlayOrPause() {
  63. console.log('切换开关')
  64. if (this.isPlay) {
  65. this.playerInstance?.pause("c_player_pause");
  66. } else {
  67. this.playerInstance?.play("c_player_play");
  68. }
  69. },
  70. startDrag(event) {
  71. // 阻止默认行为,防止页面滚动等
  72. event.preventDefault();
  73. if (event.type === 'touchstart') {
  74. this.startX = event.touches[0].clientX;
  75. } else {
  76. this.startX = event.clientX;
  77. }
  78. this.isDrag = true;
  79. this.onDrag && this.onDrag(true);
  80. this.pauseTimer(); // 暂停定时器
  81. this.startProgress = this.progress;
  82. },
  83. handleTouch(event) {
  84. if (!this.isDrag) return;
  85. let currentX;
  86. if (event.type === 'touchmove') {
  87. currentX = event.touches[0].clientX;
  88. } else {
  89. currentX = event.clientX;
  90. }
  91. const diffX = currentX - this.startX;
  92. const containerWidth = this.$refs.fullProgress.clientWidth;
  93. this.progress = Math.min(100, Math.max(0, this.startProgress + (diffX / containerWidth) * 100));
  94. this.updateCurrentTime();
  95. },
  96. // 更新当前时间
  97. updateCurrentTime() {
  98. this.currentTime = (this.totalTime * this.progress) / 100;
  99. },
  100. // 格式化时间
  101. formatTime(seconds) {
  102. const minutes = Math.floor(seconds / 60);
  103. const secs = Math.floor(seconds % 60);
  104. return [
  105. minutes.toString().padStart(2, '0'),
  106. secs.toString().padStart(2, '0')
  107. ].join(':');
  108. },
  109. // 拖拽结束
  110. handleTouchEnd() {
  111. this.onDrag && this.onDrag(false);
  112. this.isDrag = false;
  113. this.$emit('touchend', this.currentTime);
  114. // this.resumeTimer(); // 重新启动定时器
  115. this.playerInstance?.handleSeekTo(this.currentTime);
  116. },
  117. // 开始定时器
  118. startTimer() {
  119. if(this.timer) {
  120. clearInterval(this.timer);
  121. this.timer = null;
  122. }
  123. this.timer = setInterval(() => {
  124. if (this.currentTime < this.totalTime) {
  125. this.currentTime++;
  126. this.progress = (this.currentTime / this.totalTime) * 100;
  127. } else {
  128. this.stopTimer();
  129. }
  130. }, 1000);
  131. },
  132. // 暂停定时器
  133. pauseTimer() {
  134. if (this.timer) {
  135. clearInterval(this.timer);
  136. this.timer = null;
  137. }
  138. },
  139. // 重新开始定时器
  140. resumeTimer() {
  141. console.log("开始重新定时")
  142. if (!this.timer) {
  143. this.startTimer();
  144. }
  145. },
  146. // 停止定时器
  147. stopTimer() {
  148. if (this.timer) {
  149. clearInterval(this.timer);
  150. this.timer = null;
  151. }
  152. }
  153. },
  154. computed: {
  155. currentTimeText() {
  156. return this.formatTime(this.currentTime);
  157. },
  158. totalTimeText() {
  159. return this.formatTime(this.totalTime);
  160. }
  161. },
  162. }
  163. </script>
  164. <template>
  165. <div class="full-progress">
  166. <div class="progress-row">
  167. <span class="progress-time">{{ currentTimeText }}</span>
  168. <div
  169. @touch.stop="handleTouch"
  170. @touchend.stop="handleTouchEnd"
  171. @touchstart.stop="startDrag"
  172. ref="fullProgress"
  173. :style="{'--progress-width': progress + '%'}"
  174. class="progress"></div>
  175. <span class="progress-time">{{ totalTimeText }}</span>
  176. </div>
  177. <div class="tool-box">
  178. <img v-if="isPlay" src="@/assets/play.png" class="play-icon" @click.stop="handlePlayOrPause" />
  179. <img v-else src="@/assets/pause.png" class="play-icon" @click.stop="handlePlayOrPause" />
  180. <div class="right-tool">
  181. <div class="tool-item">
  182. <span class="tool-name">倍速</span>
  183. </div>
  184. <div class="tool-item">
  185. <span class="tool-name">1280P</span>
  186. </div>
  187. </div>
  188. </div>
  189. </div>
  190. </template>
  191. <style scoped>
  192. .full-progress {
  193. margin: 0 60px 20px;
  194. }
  195. .progress-row {
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. }
  200. .progress {
  201. flex: 1;
  202. height: 2px;
  203. background: rgba(255, 255, 255, 0.24);
  204. background: pink;
  205. border-radius: 2px;
  206. margin: 0 24px;
  207. position: relative;
  208. }
  209. .progress::before {
  210. content: "";
  211. position: absolute;
  212. border-radius: 2px;
  213. height: 2px;
  214. transition: width 0.01s ease-in-out;
  215. width: var(--progress-width);
  216. background: #EB3C37;
  217. }
  218. .progress::after {
  219. content: "";
  220. width: 8px;
  221. height: 8px;
  222. background: #FFFFFF;
  223. box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.5);
  224. border-radius: 4px;
  225. position: absolute;
  226. transition: left 0.01s ease-in-out;
  227. left: var(--progress-width);
  228. top: -4px;
  229. }
  230. .progress-time {
  231. font-weight: bold;
  232. font-size: 12px;
  233. color: #FFFFFF;
  234. min-width: 30px;
  235. }
  236. .tool-box {
  237. display: flex;
  238. align-items: center;
  239. justify-content: space-between;
  240. margin-top: 8px;
  241. }
  242. .play-icon {
  243. width: 44px;
  244. height: 44px;
  245. }
  246. .right-tool {
  247. display: flex;
  248. align-items: center;
  249. column-gap: 24px;
  250. }
  251. .tool-name {
  252. font-weight: 600;
  253. font-size: 15px;
  254. color: #FFFFFF;
  255. }
  256. </style>