VideoPlayer.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <video ref="videoPlayer" class="video-js"></video>
  3. </template>
  4. <script setup>
  5. import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
  6. import videojs from 'video.js'
  7. import 'video.js/dist/video-js.css'
  8. const props = defineProps({
  9. source: {
  10. type: Object,
  11. default: () => ({})
  12. }
  13. })
  14. const videoPlayer = ref(null)
  15. let player = null
  16. // 播放器配置
  17. const options = {
  18. playbackRates: [0.5, 1.0, 1.5, 2.0],
  19. autoplay: false,
  20. muted: false,
  21. loop: false,
  22. preload: 'auto',
  23. language: 'zh-CN',
  24. aspectRatio: '16:9',
  25. fluid: true,
  26. sources: [props.source],
  27. notSupportedMessage: '此视频暂无法播放,请稍后再试',
  28. controls: true,
  29. controlBar: {
  30. timeDivider: true,
  31. durationDisplay: true,
  32. remainingTimeDisplay: false,
  33. fullscreenToggle: true
  34. }
  35. }
  36. watch(
  37. () => props.source,
  38. (newSource) => {
  39. if (player) {
  40. player.pause()
  41. player.currentTime(0)
  42. player.src(newSource)
  43. player.play()
  44. }
  45. }
  46. )
  47. onMounted(() => {
  48. player = videojs(videoPlayer.value, options)
  49. window.videojs = videojs
  50. // 使用 require 加载中文语言包
  51. import('video.js/dist/lang/zh-CN.js')
  52. })
  53. onBeforeUnmount(() => {
  54. if (player) {
  55. player.dispose()
  56. player = null
  57. }
  58. })
  59. </script>
  60. <style lang="less" scoped>
  61. @import '@/style/myResource/varibles.less';
  62. .video-js {
  63. width: 100%;
  64. height: 100%;
  65. padding: 0;
  66. color: @primary-color;
  67. :deep(.vjs-big-play-button) {
  68. border-radius: 50%;
  69. border: 6px solid @primary-color;
  70. left: calc(50% - 1em);
  71. top: calc(50% - 1em);
  72. width: 2.5em;
  73. height: 2.5em;
  74. line-height: 2.5em;
  75. background: transparent;
  76. box-sizing: content-box;
  77. .vjs-icon-placeholder:before {
  78. font-size: 48px;
  79. }
  80. &:hover {
  81. opacity: 0.6;
  82. }
  83. }
  84. :deep(.vjs-volume-level),
  85. :deep(.vjs-play-progress),
  86. :deep(.vjs-slider-bar) {
  87. background: @primary-color;
  88. }
  89. :deep(.vjs-control-bar) {
  90. font-size: 14px;
  91. }
  92. }
  93. </style>