当前位置:   article > 正文

vue3使用西瓜播放器播放flv、hls、mp4视频_xgplayer vue3

xgplayer vue3

vue3使用西瓜播放器播放flv、hls、mp4视频

安装相关的插件

npm install xgplayer

npminstall xgplayer-flv

npm install xgplayer-hls

npm install  xgplayer-mp4

组件封装

  1. <template>
  2. <div :id="`${playerId}`" />
  3. </template>
  4. <script setup lang="ts">
  5. import Player from 'xgplayer'
  6. import FlvPlugin from 'xgplayer-flv'
  7. import HlsPlugin from 'xgplayer-hls'
  8. import Mp4Plugin from 'xgplayer-mp4'
  9. import 'xgplayer/dist/index.min.css'
  10. import { ref, watch, onMounted, onUnmounted } from 'vue'
  11. interface propsType {
  12. playerId?: string
  13. width?: number
  14. height?: number
  15. url: string
  16. plugin?: 'flv' | 'hls' | 'mp4'
  17. fitVideoSize?: 'fixed' | 'fixWidth' | 'fixHeight' | undefined
  18. controls?: boolean
  19. }
  20. const props = withDefaults(defineProps<propsType>(), {
  21. playerId: 'playerContainer',
  22. width: 640,
  23. height: 320,
  24. url: '',
  25. plugin: 'hls',
  26. fitVideoSize: 'fixWidth',
  27. controls: true
  28. })
  29. const player = ref<any>(null)
  30. const clientWidth = ref<number>(1920)
  31. const clientHeight = ref<number>(1080)
  32. onMounted(() => {
  33. init()
  34. clientWidth.value = document.body.clientWidth
  35. clientHeight.value = document.body.clientHeight
  36. window.addEventListener(
  37. 'resize',
  38. () => {
  39. clientWidth.value = document.body.clientWidth
  40. clientHeight.value = document.body.clientHeight
  41. init()
  42. },
  43. false
  44. )
  45. })
  46. watch(
  47. () => props.url,
  48. () => {
  49. init()
  50. },
  51. { deep: true }
  52. )
  53. const getPlugins = () => {
  54. let plugins = [Mp4Plugin]
  55. switch (props.plugin) {
  56. case 'hls':
  57. // @ts-expect-error version报错
  58. plugins = [HlsPlugin]
  59. break
  60. case 'flv':
  61. // @ts-expect-error version报错
  62. plugins = [FlvPlugin]
  63. break
  64. default:
  65. plugins = [Mp4Plugin]
  66. break
  67. }
  68. return plugins
  69. }
  70. const init = async () => {
  71. player.value = new Player({
  72. id: props.playerId,
  73. isLive: true,
  74. autoplayMuted: true,
  75. autoplay: true,
  76. plugins: await getPlugins(),
  77. url: props.url,
  78. fitVideoSize: props.fitVideoSize,
  79. height: props.height * (clientHeight.value / 1080),
  80. width: props.width * (clientWidth.value / 1920),
  81. lang: 'zh-cn',
  82. controls: props.controls
  83. })
  84. }
  85. /**
  86. * 销毁播放器
  87. */
  88. onUnmounted(() => {
  89. player.value.destroy()
  90. })
  91. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/256367
推荐阅读
相关标签
  

闽ICP备14008679号