LivePlayer.vue 4.1 KB

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