LivePlayerWrapper.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <div ref="videoEl" style="width: 100%; height: 100%">
  3. <live-player ref="player" :video-url="src" live stretch autoplay muted controls style="width: 100%; height: 100%" @error="handleError" />
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import LivePlayer from '@liveqing/liveplayer-v3';
  8. import { useDrag } from '/@/hooks/event/useDrag';
  9. import { onBeforeUnmount, onMounted, onUnmounted, ref } from 'vue';
  10. // 注册组件
  11. defineProps<{
  12. src: string;
  13. }>();
  14. const videoEl = ref<HTMLElement | null>(null);
  15. const player = ref<LivePlayer | null>(null);
  16. let retryCount = 0;
  17. const maxRetry = 30; // 最大重试次数
  18. const retryInterval = 3000; // 重试间隔(毫秒)
  19. let timer: NodeJS.Timeout | null = null;
  20. function handleError(err) {
  21. if (retryCount < maxRetry) {
  22. retryCount++;
  23. console.log(`第 ${retryCount} 次重试...`);
  24. timer = setTimeout(() => {
  25. if (player.value && player.value.play) player.value.play();
  26. }, retryInterval);
  27. } else {
  28. console.error('重试次数已达上限,停止重连');
  29. // 可在此处显示“连接失败,请刷新”提示
  30. showReconnectButton();
  31. }
  32. }
  33. function showReconnectButton() {
  34. if (player.value)
  35. player.value.innerHTML = `
  36. <div style="color: white; text-align: center; padding: 20px;">
  37. 视频连接失败<br>
  38. <button onclick="location.reload()" style="margin-top: 10px; padding: 8px 16px;">点击重试</button>
  39. </div>
  40. `;
  41. }
  42. onMounted(() => {
  43. if (videoEl.value) useDrag(videoEl.value);
  44. });
  45. onBeforeUnmount(() => {
  46. if (timer) clearTimeout(timer);
  47. if (player.value && player.value.destroy) player.value.destroy();
  48. if (player.value && player.value.remove) player.value.remove();
  49. });
  50. </script>
  51. <style scoped>
  52. /* 添加scoped样式避免影响其他组件 */
  53. </style>