useInitPlayer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { ref, onMounted, watch } from 'vue'
  2. import { postMessage } from '../utils/iframePip.js'
  3. import Player, { Events } from 'xgplayer'
  4. import 'xgplayer/dist/index.min.css'
  5. // xgplay相关的
  6. const useInitPlayer = (props, emits) => {
  7. const xgplayer = ref(null)
  8. // 是否全屏
  9. const isCssFullScreenRef = ref(false)
  10. // 是否展示问题
  11. const questionShowRef = ref(false)
  12. // 视频历史记录
  13. const historyListRef = ref([])
  14. // 是否是移动端设备
  15. const isMobileDevice = () => {
  16. return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
  17. navigator.userAgent
  18. )
  19. }
  20. // 是否是手机
  21. const isMobile = isMobileDevice()
  22. // 获取全屏设置
  23. const getFullscreen = () => {
  24. const defaultConfig = {
  25. needBackIcon: true
  26. }
  27. return isMobile
  28. ? {
  29. rotateFullscreen: true,
  30. ...defaultConfig
  31. }
  32. : {
  33. useCssFullscreen: true,
  34. ...defaultConfig
  35. }
  36. }
  37. // 播放器容器对外发送信息
  38. const viewerPostMessage = (data) => {
  39. props.ifSendMsg && postMessage(data)
  40. }
  41. // 初始化播放器
  42. const initPlay = () => {
  43. historyListRef.value = []
  44. return new Player({
  45. id: 'mse',
  46. lang: 'zh',
  47. url: props.videoUrl,
  48. height: props.height,
  49. width: props.width,
  50. // 自动播放
  51. autoplay: props.autoplay,
  52. // 静音播放
  53. autoplayMuted: props.autoplayMuted,
  54. // 封面图片
  55. poster: props.poster,
  56. // 全屏插件配置
  57. fullscreen: getFullscreen(),
  58. // 需要忽略的插件
  59. ignores: ['cssfullscreen']
  60. })
  61. }
  62. const playNext = (url, poster) => {
  63. // 关闭问题
  64. questionShowRef.value = false
  65. // 播放下一个视频
  66. xgplayer.value.playNext({
  67. url: url
  68. })
  69. // 设置封面,playNext里面设置有点问题,时常不生效
  70. xgplayer.value.setConfig({
  71. poster: poster
  72. })
  73. }
  74. // 初始化事件
  75. const initHandler = (xgplayer) => {
  76. if (!xgplayer) return
  77. // 全屏变化
  78. xgplayer.on(Events.FULLSCREEN_CHANGE, (isFullScreen) => {
  79. isCssFullScreenRef.value = isFullScreen
  80. emits('onFullscreenChange', isFullScreen)
  81. })
  82. // 视频播放结束
  83. xgplayer.on(Events.ENDED, () => {
  84. questionShowRef.value = true
  85. })
  86. // 视频开始播放
  87. xgplayer.on(Events.PLAY, () => {
  88. questionShowRef.value = false
  89. })
  90. // 视频资源加载成功
  91. xgplayer.on(Events.LOADED_DATA, () => {
  92. console.log('视频资源加载成功')
  93. // 表示视频切换后加载成功
  94. if (historyListRef.value.length > 0) {
  95. xgplayer.currentTime = 0
  96. setTimeout(() => {
  97. xgplayer
  98. .play()
  99. .then(() => {
  100. // 播放成功
  101. console.log('播放成功')
  102. })
  103. .catch((err) => {
  104. console.log('播放失败', err)
  105. // 播放失败,一般发生于未经用户交互时的自动播放
  106. })
  107. }, 50)
  108. }
  109. historyListRef.value.push(props.videoUrl)
  110. })
  111. }
  112. const init = () => {
  113. if (!props.videoUrl) return
  114. console.log('开始初始化视频播放器')
  115. try {
  116. xgplayer.value = initPlay()
  117. initHandler(xgplayer.value)
  118. viewerPostMessage({
  119. msgType: 'video_init',
  120. content: '视频播放器初始化成功'
  121. })
  122. } catch (error) {
  123. viewerPostMessage({
  124. msgType: 'video_init_fail',
  125. content: '视频播放器初始失败'
  126. })
  127. }
  128. }
  129. watch(
  130. () => props.videoUrl,
  131. (val, oldVal) => {
  132. // 首次加载视频进行初始化
  133. if (val && !oldVal) init()
  134. }
  135. )
  136. onMounted(() => {
  137. init()
  138. setTimeout(() => {
  139. viewerPostMessage({
  140. msgType: 'video_ready',
  141. content: '视频容器准备就绪'
  142. })
  143. }, 250)
  144. })
  145. return {
  146. xgplayer,
  147. isMobile,
  148. isCssFullScreenRef,
  149. questionShowRef,
  150. playNext
  151. }
  152. }
  153. export default useInitPlayer