LivePlayer.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div class="live-player">
  3. <video v-if="videoParam.liveType == 2" ref="videoPlayer" loop autoplay class="player">
  4. <source :src="videoParam.videoUrl" type="application/x-mpegURL">
  5. </video>
  6. <video v-if="videoParam.liveType == 1" ref="livePlayer" loop autoplay class="player">
  7. </video>
  8. </div>
  9. </template>
  10. <script>
  11. import Hls from "hls.js";
  12. export default {
  13. name: 'LivePlayer',
  14. props: {
  15. videoParam: {
  16. type: Object,
  17. required: true,
  18. default: () => ({
  19. startTime: '',
  20. livingUrl: '',
  21. liveType: 1,
  22. videoUrl: ''
  23. }),
  24. }
  25. },
  26. data() {
  27. return {
  28. videoDuration: 0, // 视频总时长(秒)
  29. startTime: 0, // 开播时间戳(毫秒)
  30. hls: null, // HLS 实例
  31. };
  32. },
  33. mounted() {
  34. // 这里可以添加播放器初始化逻辑
  35. },
  36. methods: {
  37. videoPlay(url) {
  38. if (Hls.isSupported()) {
  39. const videoElement = this.$refs.videoPlayer
  40. if (!videoElement) {
  41. console.error('找不到 video 元素')
  42. return
  43. }
  44. this.hls = new Hls();
  45. this.hls.attachMedia(videoElement);
  46. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  47. this.hls.loadSource(url);
  48. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  49. videoElement.play();
  50. });
  51. });
  52. this.hls.on(Hls.Events.ERROR, (event, data) => {
  53. console.error('HLS 错误:', data);
  54. });
  55. // 1. 初始化开播时间
  56. this.startTime = new Date(this.videoParam.startTime).getTime();
  57. if (this.videoParam.liveType === 2) {
  58. // 2. 监听视频元数据加载完成(获取视频时长)
  59. videoElement.addEventListener('loadedmetadata', () => {
  60. this.videoDuration = videoElement.duration; // 获取视频时长(秒)
  61. // 初始化视频播放位置
  62. this.updateVideoPosition(videoElement);
  63. });
  64. }
  65. } else {
  66. console.error('浏览器不支持 HLS')
  67. }
  68. },
  69. updateVideoPosition(video){
  70. const currentTime = new Date().getTime(); // 当前时间戳(毫秒)
  71. const elapsedTime = currentTime - this.startTime; // 已流逝时间(毫秒)
  72. if (elapsedTime < 0) {
  73. // 未开播:视频停在初始位置
  74. video.currentTime = 0;
  75. return;
  76. }
  77. // 已开播:计算视频循环后的位置(流逝时间 % 视频时长)
  78. const elapsedSeconds = elapsedTime / 1000; // 转换为秒
  79. // 视频内的播放位置(秒)
  80. // 设置视频播放位置
  81. video.currentTime = elapsedSeconds % this.videoDuration;
  82. },
  83. livePlay(url) {
  84. if (Hls.isSupported()) {
  85. const videoElement = this.$refs.livePlayer
  86. if (!videoElement) {
  87. console.error('找不到 video 元素')
  88. return
  89. }
  90. this.hls = new Hls();
  91. this.hls.attachMedia(videoElement);
  92. this.hls.on(Hls.Events.MEDIA_ATTACHED, () => {
  93. this.hls.loadSource(url);
  94. this.hls.on(Hls.Events.STREAM_LOADED, (event, data) => {
  95. videoElement.play();
  96. });
  97. });
  98. this.hls.on(Hls.Events.ERROR, (event, data) => {
  99. console.error('HLS 错误:', data);
  100. });
  101. } else {
  102. console.error('浏览器不支持 HLS')
  103. }
  104. },
  105. initPlayer() {
  106. if (this.videoParam.liveType === 1) {
  107. // 直播
  108. const isUrl = this.videoParam.livingUrl === null || this.videoParam.livingUrl.trim() === '';
  109. if (isUrl) {
  110. console.error('直播地址为空,无法初始化播放器')
  111. return
  112. }
  113. this.$nextTick(() => {
  114. this.livePlay(this.videoParam.livingUrl);
  115. })
  116. return;
  117. }
  118. const viUrl = this.videoParam.videoUrl === null || this.videoParam.videoUrl.trim() === ''
  119. if (viUrl) {
  120. console.error('播放地址为空,无法初始化播放器')
  121. return
  122. }
  123. if(this.videoParam.liveType === 2){
  124. // 录播
  125. this.videoPlay(this.videoParam.videoUrl);
  126. } else if(this.videoParam.liveType === 3){
  127. // 直播回放
  128. this.videoPlay(this.videoParam.videoUrl);
  129. }else {
  130. console.error('直播类型错误,无法初始化播放器')
  131. }
  132. // 创建播放器实例
  133. const videoPlayer = this.$refs.videoPlayer;
  134. // 添加播放器事件监听器
  135. videoPlayer.addEventListener('play', () => {
  136. console.log('播放器已开始播放');
  137. });
  138. videoPlayer.addEventListener('pause', () => {
  139. console.log('播放器已暂停');
  140. });
  141. videoPlayer.addEventListener('ended', () => {
  142. console.log('播放器已结束');
  143. });
  144. }
  145. },
  146. beforeDestroy() {
  147. this.hls?.destroy()
  148. // 销毁播放器实例
  149. }
  150. };
  151. </script>
  152. <style scoped>
  153. .live-player {
  154. margin-bottom: 20px;
  155. }
  156. .player {
  157. width: 100%;
  158. height: auto;
  159. border-radius: 8px;
  160. max-height: 300px; /* 设置最大高度,避免过大 */
  161. object-fit: contain; /* 保持比例,不拉伸 */
  162. background-color: #000; /* 黑色背景,避免视频加载时显示白色 */
  163. }
  164. </style>