index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!--
  2. * @Descripttion: xgplayer二次封装
  3. * @version: 1.1
  4. * @Date: 2021年11月29日12:10:06
  5. * @LastEditTime: 2023年12月22日12:02:50
  6. -->
  7. <template>
  8. <div class="sc-video" ref="scVideo">
  9. <div v-if="showMask" class="sc-video__start-mock" @click="$emit('play')"></div>
  10. </div>
  11. </template>
  12. <script>
  13. import Player from 'xgplayer';
  14. import HlsPlayer from 'xgplayer-hls';
  15. export default {
  16. emits: ["play"],
  17. props: {
  18. showMask: { type: Boolean, default: false },
  19. src: { type: String, required: true, default: "" },
  20. autoplay: { type: Boolean, default: false },
  21. controls: { type: Boolean, default: true },
  22. loop: { type: Boolean, default: false },
  23. isLive: { type: Boolean, default: false },
  24. options: { type: Object, default: () => {} }
  25. },
  26. data() {
  27. return {
  28. player: null
  29. }
  30. },
  31. watch: {
  32. src(val) {
  33. if (this.player.hasStart) this.player.src = val;
  34. else this.player.start(val);
  35. }
  36. },
  37. mounted() {
  38. if (this.isLive) this.initHls();
  39. else this.init();
  40. },
  41. methods: {
  42. init() {
  43. this.player = new Player({
  44. el: this.$refs.scVideo,
  45. url: this.src,
  46. autoplay: this.autoplay,
  47. loop: this.loop, // 循环播放
  48. controls: this.controls,
  49. fluid: true, // 播放器宽度跟随父元素的宽度大小变化
  50. videoInit: true, // 初始化显示视频首帧
  51. lang: 'zh-cn',
  52. ...this.options
  53. });
  54. },
  55. initHls() {
  56. this.player = new HlsPlayer({
  57. el: this.$refs.scVideo,
  58. url: this.src,
  59. autoplay: this.autoplay,
  60. loop: this.loop,
  61. controls: this.controls,
  62. fluid: true,
  63. videoInit: true, // 初始化显示视频首帧
  64. isLive: true,
  65. ignores: ['time','progress'],
  66. lang: 'zh-cn',
  67. ...this.options
  68. });
  69. }
  70. }
  71. }
  72. </script>
  73. <style scoped>
  74. .sc-video:deep(.danmu) > * {
  75. color: #fff;
  76. font-size: 20px;
  77. font-weight: bold;
  78. text-shadow: 1px 1px 0 #000, -1px -1px 0 #000, -1px 1px 0 #000,
  79. 1px -1px 0 #000;
  80. }
  81. .sc-video__start-mock {
  82. cursor: pointer;
  83. width: 70px;
  84. height: 70px;
  85. position: absolute;
  86. left: calc(50% - 35px);
  87. top: calc(50% - 35px);
  88. z-index: 120;
  89. }
  90. .sc-video:deep(.xgplayer-controls) {
  91. background-image: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.3));
  92. }
  93. .sc-video:deep(.xgplayer-progress-tip) {
  94. border: 0;
  95. color: #fff;
  96. background: rgba(0, 0, 0, 0.5);
  97. line-height: 25px;
  98. padding: 0 10px;
  99. border-radius: 25px;
  100. }
  101. .sc-video:deep(.xgplayer-enter-spinner) {
  102. width: 50px;
  103. height: 50px;
  104. }
  105. </style>