Player.vue 962 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <template>
  2. <div class="player">
  3. <h2 style="margin-bottom: 12px;">{{ route.query.title || '直播播放' }}</h2>
  4. <video
  5. ref="videoRef"
  6. v-if="videoUrl"
  7. controls
  8. autoplay
  9. style="width: 100%; max-width: 100%; border-radius: 8px;"
  10. ></video>
  11. <div v-else class="empty">暂无可播放的视频</div>
  12. </div>
  13. </template>
  14. <script setup>
  15. import { onMounted, ref } from 'vue'
  16. import { useRoute } from 'vue-router'
  17. import Hls from 'hls.js'
  18. const route = useRoute()
  19. const videoUrl = route.query.videoUrl
  20. const videoRef = ref(null)
  21. onMounted(() => {
  22. if (videoUrl && Hls.isSupported()) {
  23. const hls = new Hls()
  24. hls.loadSource(videoUrl)
  25. hls.attachMedia(videoRef.value)
  26. } else if (videoRef.value) {
  27. // 对于 Safari 原生支持 .m3u8
  28. videoRef.value.src = videoUrl
  29. }
  30. })
  31. </script>
  32. <style scoped>
  33. .player {
  34. padding: 16px;
  35. }
  36. .empty {
  37. text-align: center;
  38. color: #999;
  39. margin-top: 40px;
  40. }
  41. </style>