liveVideo.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <template>
  2. <view class="video-player-container">
  3. <!-- 直播状态显示 -->
  4. <view class="videolist" v-if="liveItem.status == 2">
  5. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  6. <!-- 直播 -->
  7. <live-player
  8. v-if="liveItem.livingUrl && liveItem.liveType == 1"
  9. :id="'myLivePlayer_' + liveId"
  10. :src="liveItem.livingUrl"
  11. autoplay
  12. mode="live"
  13. object-fit="contain"
  14. :muted="false"
  15. orientation="vertical"
  16. :enable-play-gesture="false"
  17. min-cache="1"
  18. max-cache="3"
  19. @statechange="onLiveStateChange"
  20. @error="onLiveError"
  21. class="item"></live-player>
  22. <!-- 录播 -->
  23. <video
  24. v-if="liveItem.videoUrl && liveItem.liveType == 2"
  25. :id="`myVideo_${liveId}`"
  26. :autoplay="true"
  27. class="item"
  28. :src="liveItem.videoUrl"
  29. :controls="false"
  30. object-fit="contain"
  31. :custom-cache="false"
  32. :enable-progress-gesture="false"
  33. vslide-gesture-in-fullscreen="false"
  34. :show-center-play-btn="false"
  35. :http-cache="false"
  36. loop
  37. @error="videoError"
  38. @timeupdate="onVideoTimeUpdate"
  39. @loadedmetadata="onVideoMetaLoaded"
  40. @pause="onVideoPause"
  41. @play="onVideoPlay"
  42. @waiting="onVideoWaiting"
  43. :enable-play-gesture="false"
  44. :play-strategy="1"
  45. @dblclick="preventDoubleClick"
  46. preload="auto"
  47. :enable-stash-buffer="false"
  48. :stash-initial-size="0"
  49. :stash-max-size="0"
  50. :stash-time="0"
  51. type="application/x-mpegURL"></video>
  52. <view v-if="liveItem.videoUrl && liveItem.liveType == 2" class="time">{{ diffTotalTime }}</view>
  53. </view>
  54. </view>
  55. <!-- 直播结束状态 -->
  56. <view class="videolist" v-if="liveItem.status == 3">
  57. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  58. <view class="end">直播已结束</view>
  59. </view>
  60. </view>
  61. <!-- 直播回放状态 -->
  62. <view class="videolist" v-if="liveItem.status == 4">
  63. <view class="video" :class="liveItem.showType == 1 ? 'video_row' : ''">
  64. <!-- 直播回放 -->
  65. <video
  66. v-if="liveItem.videoUrl && liveItem.liveType == 3"
  67. :id="`myVideo_${liveId}`"
  68. class="item"
  69. :src="liveItem.videoUrl"
  70. :autoplay="true"
  71. :controls="true"
  72. object-fit="contain"
  73. :custom-cache="false"
  74. :enable-progress-gesture="liveItem.isSpeedAllowed"
  75. vslide-gesture-in-fullscreen="true"
  76. :show-center-play-btn="true"
  77. :http-cache="false"
  78. loop
  79. @error="videoError"
  80. @timeupdate="onVideoTimeUpdate"
  81. @loadedmetadata="onVideoMetaLoaded"
  82. @pause="onVideoPause"
  83. @play="onVideoPlay"
  84. :enable-play-gesture="true"
  85. preload="auto"
  86. @waiting="onVideoWaiting"
  87. type="application/x-mpegURL"></video>
  88. <view v-if="liveItem.videoUrl && liveItem.liveType == 3" class="lable">直播回放</view>
  89. </view>
  90. </view>
  91. <!-- 直播预告状态 -->
  92. <view class="trailer-box" v-if="liveItem.status == 1">
  93. <video
  94. v-if="liveItem.previewUrl"
  95. :id="`myVideo_${liveId}`"
  96. class="item"
  97. :src="liveItem.previewUrl"
  98. :autoplay="true"
  99. :loop="false"
  100. :controls="false"
  101. object-fit="contain"
  102. :custom-cache="false"
  103. :enable-progress-gesture="false"
  104. vslide-gesture-in-fullscreen="false"
  105. :show-center-play-btn="false"
  106. :http-cache="false"
  107. @error="videoError"
  108. @loadedmetadata="onVideoMetaLoaded"
  109. @pause="onVideoPause"
  110. @play="onVideoPlay"
  111. :disable-progress="true"
  112. :enable-play-gesture="true"
  113. @waiting="onVideoWaiting"
  114. preload="auto"
  115. type="application/x-mpegURL"></video>
  116. <image
  117. v-if="liveItem.status == 1 && !liveItem.previewUrl"
  118. class="img"
  119. src="/static/images/no_live.png"></image>
  120. <view class="countdown-item mt30 mb30" v-if="liveItem.status == 1 && liveCountdown">
  121. 开播倒计时
  122. <view class="x-f">
  123. <view class="white">{{ liveCountdown.hours || '00' }}</view>
  124. <view class="white">{{ liveCountdown.minutes || '00' }}</view>
  125. <view class="white">{{ liveCountdown.seconds || '00' }}</view>
  126. </view>
  127. </view>
  128. <view class="name">{{ liveItem.liveName }}</view>
  129. <view class="title" v-if="liveCountdown">暂未开播</view>
  130. <view class="button" v-if="!isAgreement" @click="handleAgreement">预约直播</view>
  131. <view class="button ash" v-if="isAgreement">已预约</view>
  132. <view class="title" v-if="!liveCountdown">主播还在来的路上</view>
  133. </view>
  134. <!-- 无直播状态 -->
  135. <view class="trailer-box" v-if="!liveItem">
  136. <image class="img" src="/static/images/no_live.png"></image>
  137. <view class="title">暂无直播</view>
  138. </view>
  139. </view>
  140. </template>
  141. <script>
  142. import { generateRandomString } from '@/utils/common.js';
  143. import dayjs from 'dayjs';
  144. import { internetTraffic, liveInternetTraffic } from '@/api/living.js';
  145. import { getUserInfo } from '@/api/user';
  146. export default {
  147. name: 'LiveVideoPlayer',
  148. props: {
  149. liveItem: {
  150. type: Object,
  151. default: () => ({})
  152. },
  153. liveId: {
  154. type: [String, Number],
  155. default: ''
  156. },
  157. userData: {
  158. type: Object,
  159. default: () => ({})
  160. }
  161. },
  162. data() {
  163. return {
  164. // 流量计算相关
  165. uuId: '',
  166. totalTraffic: 0,
  167. bitrate: 800, // 录播默认码率 0.16Mbps
  168. bitrateLive: 1600, // 直播默认码率 0.16Mbps
  169. // 定时器
  170. trafficTimer: null,
  171. liveStartTimer: null,
  172. trafficInterval: null,
  173. lookTimer: null,
  174. // 状态数据
  175. liveCountdown: {},
  176. diffTotalTime: '',
  177. videoCurrentTime: 0,
  178. videoProgressKey: '',
  179. startTime: 0,
  180. stayTime: 0,
  181. isAgreement: false,
  182. // 性能优化
  183. videoRetryCounts: Object.create(null)
  184. };
  185. },
  186. computed: {
  187. // 计算当前时间与开始时间的差值
  188. currentTimeDiff() {
  189. if (!this.liveItem.startTime) return 0;
  190. const timeStr = this.liveItem.startTime;
  191. const time = new Date(timeStr.replace(/-/g, '/'));
  192. if (isNaN(time.getTime())) return 0;
  193. const now = new Date();
  194. return Math.max(0, now.getTime() - time.getTime());
  195. }
  196. },
  197. watch: {
  198. 'liveItem.status': {
  199. handler(newStatus, oldStatus) {
  200. if (newStatus === undefined || oldStatus === undefined) return;
  201. if (newStatus === oldStatus) return;
  202. console.log(`直播状态变化: ${oldStatus} -> ${newStatus}`);
  203. this.handleStatusChange(newStatus);
  204. },
  205. immediate: true
  206. }
  207. // ,
  208. // 'liveItem.status': {
  209. // handler(newVal) {
  210. // if (newVal && newVal.status === 2) {
  211. // this.startTimeTimer(newVal);
  212. // }
  213. // },
  214. // deep: true,
  215. // immediate: true
  216. // }
  217. },
  218. async mounted() {
  219. await this.initializeComponent();
  220. },
  221. beforeUnmount() {
  222. this.cleanup();
  223. },
  224. methods: {
  225. // 初始化组件
  226. async initializeComponent() {
  227. this.uuId = generateRandomString(16);
  228. this.isAgreement = uni.getStorageSync('isAgreement');
  229. if (this.liveItem.status === 2) {
  230. this.startTimeTimer(this.liveItem);
  231. }
  232. if (this.liveItem.status === 1 && this.liveItem.startTime) {
  233. this.startLiveCountdown();
  234. }
  235. this.startTimer();
  236. },
  237. // 处理状态变化
  238. handleStatusChange(newStatus) {
  239. // 清理旧定时器
  240. this.cleanupTimers();
  241. switch (newStatus) {
  242. case 1: // 预告
  243. this.startLiveCountdown();
  244. break;
  245. case 2: // 直播中
  246. this.startTimeTimer(this.liveItem);
  247. this.playVideo();
  248. break;
  249. case 3: // 已结束
  250. case 4: // 回放
  251. this.pauseVideo();
  252. break;
  253. }
  254. },
  255. // 启动直播倒计时
  256. startLiveCountdown() {
  257. if (this.liveStartTimer) {
  258. clearInterval(this.liveStartTimer);
  259. }
  260. this.liveStartTimer = setInterval(() => {
  261. this.liveCountdown = this.handleTime(this.liveItem.startTime, 0);
  262. if (!this.liveCountdown) {
  263. this.$emit('liveStart');
  264. clearInterval(this.liveStartTimer);
  265. }
  266. }, 1000);
  267. },
  268. // 启动时间计时器
  269. startTimeTimer(item) {
  270. if (!item) return;
  271. // 立即计算一次
  272. this.calculateTimeDiff(item);
  273. // 每秒更新
  274. if (item.timeTimer) {
  275. clearInterval(item.timeTimer);
  276. }
  277. item.timeTimer = setInterval(() => {
  278. this.calculateTimeDiff(item);
  279. }, 1000);
  280. },
  281. // 计算时间差
  282. calculateTimeDiff(item) {
  283. if (!item.startTime) return;
  284. const time = new Date(item.startTime.replace(/-/g, '/'));
  285. if (isNaN(time.getTime())) return;
  286. const now = new Date();
  287. let diffMs = Math.max(0, now.getTime() - time.getTime());
  288. const totalSeconds = Math.floor(diffMs / 1000);
  289. const hours = this.padZero(Math.floor(totalSeconds / 3600));
  290. const minutes = this.padZero(Math.floor((totalSeconds % 3600) / 60));
  291. const seconds = this.padZero(totalSeconds % 60);
  292. this.diffTotalTime = `${hours}:${minutes}:${seconds}`;
  293. },
  294. // 补零函数
  295. padZero(num) {
  296. return num < 10 ? `0${num}` : num;
  297. },
  298. // 时间处理
  299. handleTime(time, duration) {
  300. let timeStamp;
  301. if (typeof time === 'number' && time > 0 && time < 9999999999999) {
  302. timeStamp = time;
  303. } else if (typeof time === 'string' && time.trim() !== '') {
  304. const isoTime = time.replace(' ', 'T');
  305. const date = new Date(isoTime);
  306. if (!isNaN(date.getTime())) {
  307. timeStamp = date.getTime();
  308. } else {
  309. console.error('无效的日期格式:', time);
  310. return false;
  311. }
  312. } else {
  313. console.error('time参数必须是有效的时间戳或日期字符串');
  314. return false;
  315. }
  316. const targetTimestamp = timeStamp + duration * 60 * 1000;
  317. const currentTimestamp = Date.now();
  318. const timeDiffMs = targetTimestamp - currentTimestamp;
  319. if (timeDiffMs <= 0) {
  320. return false;
  321. }
  322. const hours = Math.floor(timeDiffMs / (1000 * 60 * 60));
  323. const minutes = Math.floor((timeDiffMs % (1000 * 60 * 60)) / (1000 * 60));
  324. const seconds = Math.floor((timeDiffMs % (1000 * 60)) / 1000);
  325. const formatNum = (num) => num.toString().padStart(2, '0');
  326. return {
  327. hours: formatNum(hours),
  328. minutes: formatNum(minutes),
  329. seconds: formatNum(seconds)
  330. };
  331. },
  332. // 播放视频
  333. playVideo() {
  334. if (!this.liveItem) {
  335. console.log('liveItem 为空,无法播放视频');
  336. return;
  337. }
  338. try {
  339. // 直播流
  340. if (this.liveItem.liveType === 1 && this.liveItem.livingUrl && this.liveItem.status == 2) {
  341. const livePlayerId = `myLivePlayer_${this.liveId}`;
  342. const livePlayerContext = uni.createLivePlayerContext(livePlayerId, this);
  343. if (livePlayerContext) {
  344. livePlayerContext.play();
  345. }
  346. }
  347. // 预告视频
  348. else if (this.liveItem.status == 1 && this.liveItem.previewUrl) {
  349. const videoId = `myVideo_${this.liveId}`;
  350. const videoContext = uni.createVideoContext(videoId, this);
  351. if (videoContext) {
  352. videoContext.play();
  353. }
  354. }
  355. // 录播
  356. else if (this.liveItem.liveType === 2 && this.liveItem.videoUrl && this.liveItem.status == 2) {
  357. const videoId = `myVideo_${this.liveId}`;
  358. const videoContext = uni.createVideoContext(videoId, this);
  359. if (videoContext) {
  360. videoContext.play();
  361. }
  362. }
  363. // 回放
  364. else if (this.liveItem.liveType === 3 && this.liveItem.videoUrl && this.liveItem.status == 4) {
  365. const videoId = `myVideo_${this.liveId}`;
  366. const videoContext = uni.createVideoContext(videoId, this);
  367. if (videoContext) {
  368. videoContext.play();
  369. }
  370. }
  371. } catch (error) {
  372. console.error('播放视频失败:', error);
  373. this.$emit('playError', error);
  374. }
  375. },
  376. // 暂停视频
  377. pauseVideo() {
  378. if (!this.liveItem) return;
  379. try {
  380. if (this.liveItem.status == 1) {
  381. const videoId = `myVideo_${this.liveId}`;
  382. const videoContext = uni.createVideoContext(videoId, this);
  383. if (videoContext) {
  384. videoContext.pause();
  385. }
  386. } else if (this.liveItem.status == 2) {
  387. if (this.liveItem.liveType === 1) {
  388. const livePlayerId = `myLivePlayer_${this.liveId}`;
  389. const livePlayerContext = uni.createLivePlayerContext(livePlayerId, this);
  390. if (livePlayerContext) {
  391. livePlayerContext.pause();
  392. }
  393. } else if (this.liveItem.liveType === 2) {
  394. const videoId = `myVideo_${this.liveId}`;
  395. const videoContext = uni.createVideoContext(videoId, this);
  396. if (videoContext) {
  397. videoContext.pause();
  398. }
  399. }
  400. }
  401. } catch (error) {
  402. console.error('暂停视频失败:', error);
  403. this.$emit('pauseError', error);
  404. }
  405. },
  406. // 视频错误处理
  407. videoError(e) {
  408. if (!this.liveItem || !this.liveId) return;
  409. // 初始化重试计数
  410. if (this.videoRetryCounts[this.liveId] === undefined) {
  411. this.videoRetryCounts[this.liveId] = 0;
  412. }
  413. // 限制重试次数
  414. if (this.videoRetryCounts[this.liveId] >= 3) {
  415. console.error(`直播间 ${this.liveId} 视频加载失败,停止重试`);
  416. this.$emit('videoError', {
  417. type: 'loadFailed',
  418. liveId: this.liveId,
  419. message: '视频加载失败,请检查网络'
  420. });
  421. return;
  422. }
  423. this.videoRetryCounts[this.liveId]++;
  424. // 延迟重试
  425. setTimeout(() => {
  426. console.log(`第${this.videoRetryCounts[this.liveId]}次重试播放视频`);
  427. this.playVideo();
  428. }, 2000);
  429. this.$emit('videoError', {
  430. type: 'retry',
  431. liveId: this.liveId,
  432. retryCount: this.videoRetryCounts[this.liveId]
  433. });
  434. },
  435. // 直播状态变化
  436. onLiveStateChange(e) {
  437. const stateCode = e.detail.code;
  438. if (e.detail.code == -2301 || e.detail.code == -2302) {
  439. this.playVideo();
  440. } else if (e.detail.code == 2004) {
  441. this.calculateTimeDiff(this.liveItem);
  442. this.startTrafficCalculation(this.bitrateLive);
  443. }
  444. this.$emit('liveStateChange', e.detail);
  445. },
  446. // 直播错误
  447. onLiveError(e) {
  448. this.videoError(e);
  449. this.$emit('liveError', e.detail);
  450. },
  451. // 视频元数据加载
  452. onVideoMetaLoaded(e) {
  453. this.videoProgressKey = `videoProgress_${this.liveId}`;
  454. this.setVideoProgress();
  455. this.$emit('videoMetaLoaded', e.detail);
  456. },
  457. // 设置视频进度
  458. setVideoProgress() {
  459. if (this.liveItem.liveType !== 2 && this.liveItem.liveType !== 3) {
  460. return;
  461. }
  462. let currentTime = 0;
  463. if (this.liveItem.liveType === 2) {
  464. const diff = this.getTimeDifferenceInSeconds(this.liveItem.startTime);
  465. if (diff > this.liveItem.duration) {
  466. const storedProgress = uni.getStorageSync(this.videoProgressKey) || 0;
  467. currentTime = storedProgress >= this.liveItem.duration ? 0 : storedProgress || 0;
  468. } else {
  469. currentTime = diff % this.liveItem.duration;
  470. }
  471. } else if (this.liveItem.liveType === 3) {
  472. const storedProgress = uni.getStorageSync(this.videoProgressKey);
  473. currentTime = storedProgress || 0;
  474. }
  475. const videoId = `myVideo_${this.liveId}`;
  476. const videoContext = uni.createVideoContext(videoId, this);
  477. if (videoContext) {
  478. videoContext.seek(currentTime);
  479. }
  480. },
  481. // 获取时间差(秒)
  482. getTimeDifferenceInSeconds(createTimeStr) {
  483. const createTime = new Date(createTimeStr.replace(/-/g, '/'));
  484. const now = new Date();
  485. const timeDiffMs = now - createTime;
  486. return Math.max(0, Math.floor(timeDiffMs / 1000));
  487. },
  488. // 视频时间更新
  489. onVideoTimeUpdate(e) {
  490. this.videoCurrentTime = e.detail.currentTime;
  491. if (Math.floor(this.videoCurrentTime) % 10 === 0) {
  492. this.saveVideoProgress();
  493. }
  494. this.$emit('videoTimeUpdate', e.detail);
  495. },
  496. // 视频暂停
  497. onVideoPause(e) {
  498. if (this.liveItem.liveType === 2) {
  499. const videoId = `myVideo_${this.liveId}`;
  500. const videoContext = uni.createVideoContext(videoId, this);
  501. setTimeout(() => {
  502. videoContext.play();
  503. }, 100);
  504. }
  505. this.saveVideoProgress();
  506. this.$emit('videoPause', e.detail);
  507. },
  508. // 视频播放
  509. onVideoPlay(e) {
  510. this.$emit('videoPlay', e.detail);
  511. },
  512. // 视频等待
  513. onVideoWaiting(e) {
  514. if (this.liveItem.liveType == 2) {
  515. this.startTrafficCalculation(this.bitrate);
  516. } else {
  517. if (this.trafficInterval) {
  518. clearInterval(this.trafficInterval);
  519. this.trafficInterval = null;
  520. }
  521. this.trafficInterval = setInterval(() => {
  522. this.getInternetTraffic();
  523. }, 10000);
  524. }
  525. this.$emit('videoWaiting', e.detail);
  526. },
  527. // 阻止双击事件
  528. preventDoubleClick(e) {
  529. e.preventDefault();
  530. e.stopPropagation();
  531. return false;
  532. },
  533. // 保存视频进度
  534. saveVideoProgress() {
  535. if (this.videoProgressKey) {
  536. uni.setStorage({
  537. key: this.videoProgressKey,
  538. data: this.videoCurrentTime,
  539. success: () => {},
  540. fail: (err) => {
  541. console.error('保存视频进度失败:', err);
  542. }
  543. });
  544. }
  545. },
  546. // 开始流量计算
  547. startTrafficCalculation(bitrate) {
  548. if (this.trafficTimer) {
  549. clearInterval(this.trafficTimer);
  550. this.trafficTimer = null;
  551. }
  552. this.startTime = Date.now();
  553. this.trafficTimer = setInterval(() => {
  554. this.calculateTraffic(bitrate);
  555. }, 10000);
  556. },
  557. // 计算流量
  558. calculateTraffic(bitrate) {
  559. const currentTime = Date.now();
  560. const duration = (currentTime - this.startTime) / 1000;
  561. const trafficBits = bitrate * duration;
  562. this.totalTraffic = trafficBits / 8;
  563. this.getLiveInternetTraffic();
  564. },
  565. // 直播流量统计
  566. getLiveInternetTraffic() {
  567. if (!this.liveId || !this.userData.userId) return;
  568. const param = {
  569. userId: this.userData.userId,
  570. liveId: this.liveId,
  571. uuId: dayjs().format('YYYYMMDD') + this.uuId,
  572. internetTraffic: this.totalTraffic
  573. };
  574. liveInternetTraffic(param).catch(err => {
  575. console.error('直播流量统计失败:', err);
  576. });
  577. },
  578. // 回放、预告流量统计
  579. getInternetTraffic() {
  580. if (!this.liveId || !this.userData.userId || !this.uuId) return;
  581. const currentTime = (this.stayTime / this.liveItem.duration) * 100;
  582. const param = {
  583. videoType: this.liveItem.videoType,
  584. videoId: this.liveItem.videoId,
  585. userId: this.userData.userId,
  586. liveId: this.liveId,
  587. uuId: dayjs().format('YYYYMMDD') + this.uuId,
  588. duration: this.liveItem.duration,
  589. bufferRate: currentTime
  590. };
  591. if (this.liveItem.status == 1) {
  592. param.videoType = this.liveItem.previewVideoType || '';
  593. param.videoId = this.liveItem.previewVideoId || '';
  594. }
  595. if (this.liveItem.liveType == 1) {
  596. param.bufferRate = this.totalTraffic;
  597. }
  598. internetTraffic(param).catch(err => {
  599. console.error('流量统计失败:', err);
  600. });
  601. },
  602. // 开始计时器
  603. startTimer() {
  604. this.startTime = Date.now();
  605. this.lookTimer = setInterval(() => {
  606. this.stayTime = Math.floor((Date.now() - this.startTime) / 1000);
  607. }, 1000);
  608. },
  609. // 预约直播
  610. handleAgreement() {
  611. this.$emit('agreementClick');
  612. },
  613. // 清理定时器
  614. cleanupTimers() {
  615. if (this.liveStartTimer) {
  616. clearInterval(this.liveStartTimer);
  617. this.liveStartTimer = null;
  618. }
  619. if (this.trafficTimer) {
  620. clearInterval(this.trafficTimer);
  621. this.trafficTimer = null;
  622. }
  623. if (this.trafficInterval) {
  624. clearInterval(this.trafficInterval);
  625. this.trafficInterval = null;
  626. }
  627. if (this.lookTimer) {
  628. clearInterval(this.lookTimer);
  629. this.lookTimer = null;
  630. }
  631. if (this.liveItem && this.liveItem.timeTimer) {
  632. clearInterval(this.liveItem.timeTimer);
  633. this.liveItem.timeTimer = null;
  634. }
  635. },
  636. // 组件清理
  637. cleanup() {
  638. this.cleanupTimers();
  639. this.pauseVideo();
  640. this.saveVideoProgress();
  641. }
  642. }
  643. };
  644. </script>
  645. <style scoped lang="scss">
  646. .video-player-container {
  647. width: 100%;
  648. height: 100%;
  649. position: relative;
  650. }
  651. .videolist {
  652. position: relative;
  653. height: 100vh;
  654. width: 100%;
  655. .video {
  656. height: 100vh;
  657. width: 100%;
  658. .item {
  659. width: 100%;
  660. height: 100%;
  661. }
  662. .time {
  663. color: #ffffff;
  664. font-size: 20rpx;
  665. margin-left: 10rpx;
  666. }
  667. .end {
  668. position: absolute;
  669. top: 50%;
  670. left: 50%;
  671. transform: translate(-50%, -50%);
  672. font-size: 36rpx;
  673. color: #fff;
  674. }
  675. .lable {
  676. position: absolute;
  677. top: 50rpx;
  678. right: 16rpx;
  679. background-color: rgba(57, 57, 57, 0.6);
  680. padding: 4rpx 10rpx;
  681. color: #fff;
  682. border-radius: 15rpx;
  683. }
  684. }
  685. .video_row {
  686. position: absolute;
  687. top: 20%;
  688. max-height: 450rpx;
  689. z-index: 99;
  690. }
  691. }
  692. .trailer-box {
  693. position: relative;
  694. top: 15%;
  695. display: flex;
  696. flex-direction: column;
  697. align-items: center;
  698. color: #ffffff;
  699. padding: 20rpx;
  700. .button {
  701. margin-top: 20rpx;
  702. background: #6d7bd4;
  703. color: #fff;
  704. border-radius: 20rpx;
  705. padding: 20rpx 30rpx;
  706. position: relative;
  707. z-index: 99999;
  708. }
  709. .ash {
  710. background: #636363;
  711. }
  712. .countdown-item {
  713. display: flex;
  714. flex-direction: column;
  715. align-items: center;
  716. .white {
  717. width: 60rpx;
  718. height: 60rpx;
  719. text-align: center;
  720. overflow: hidden;
  721. background: #ffffff;
  722. border-radius: 8rpx;
  723. margin: 30rpx 10rpx 0;
  724. font-weight: 600;
  725. font-size: 24rpx;
  726. color: #000000;
  727. line-height: 60rpx;
  728. }
  729. }
  730. .item {
  731. width: 100%;
  732. height: 400rpx;
  733. }
  734. .name {
  735. font-size: 34rpx;
  736. }
  737. .img {
  738. margin-bottom: 40rpx;
  739. width: 240rpx;
  740. height: 240rpx;
  741. }
  742. .title {
  743. margin-top: 30rpx;
  744. font-size: 42rpx;
  745. font-weight: 500;
  746. }
  747. }
  748. </style>