LivePlayer.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div id="dplayer" ref="dplayer" style="height: 480px;" />
  3. </template>
  4. <script>
  5. import { videoUrl } from '@/api/video'
  6. import SocketInstance from '@/utils/ws/socket-instance'
  7. import flvjs from 'flv.js'
  8. import DPlayer from 'dplayer'
  9. export default {
  10. name: 'VideoPlayer',
  11. props: {
  12. videoProp: {
  13. type: Object,
  14. default: () => null
  15. }
  16. },
  17. data() {
  18. return {
  19. flvjs,
  20. DPlayer,
  21. danmaku: {
  22. api: '//api.reghao.cn/api/comment/danmaku/',
  23. token: 'bili'
  24. },
  25. getUrl: true
  26. }
  27. },
  28. created() {
  29. },
  30. mounted() {
  31. // SocketInstance.connect()
  32. const videoId = this.videoProp.videoId
  33. if (this.getUrl) {
  34. // this.getVideoUrl(videoId)
  35. const url = 'https://disk.reghao.cn/live/cam1'
  36. this.initFlvPlayer(videoId, this.videoProp.coverUrl, url)
  37. }
  38. },
  39. methods: {
  40. getVideoUrl(videoId) {
  41. videoUrl(videoId).then(res => {
  42. if (res.code === 0) {
  43. const urlType = res.data.type
  44. if (urlType === 'mp4') {
  45. const urls = res.data.urls
  46. for (const url of urls) {
  47. url.type = 'normal'
  48. }
  49. const autoPlay = false
  50. this.initMp4Player(this.videoProp.userId, videoId, this.videoProp.coverUrl, urls, res.data.currentTime, autoPlay)
  51. } else if (urlType === 'flv') {
  52. const urls = res.data.urls
  53. const url = urls[0].url
  54. this.initFlvPlayer(videoId, this.videoProp.coverUrl, url)
  55. } else {
  56. this.$notify.error({
  57. message: '视频 url 类型不合法',
  58. type: 'warning',
  59. duration: 3000
  60. })
  61. }
  62. } else {
  63. this.$notify.error({
  64. message: '视频 url 获取失败',
  65. type: 'warning',
  66. duration: 3000
  67. })
  68. }
  69. }).catch(error => {
  70. this.$notify.error({
  71. message: '视频 url 获取失败',
  72. type: 'warning',
  73. duration: 3000
  74. })
  75. })
  76. },
  77. danmakuConfig() {
  78. // TODO 获取弹幕配置,将 videoUrl 作为本函数的回调
  79. },
  80. initMp4Player(userId, videoId, coverUrl, urls, pos, autoPlay) {
  81. const player = new DPlayer({
  82. container: document.querySelector('#dplayer'),
  83. lang: 'zh-cn',
  84. logo: '/logo.png',
  85. screenshot: false,
  86. autoplay: autoPlay,
  87. volume: 0.1,
  88. mutex: true,
  89. video: {
  90. pic: coverUrl,
  91. defaultQuality: 0,
  92. quality: urls
  93. },
  94. danmaku: {
  95. id: videoId,
  96. maximum: 10000,
  97. api: this.danmaku.api,
  98. token: this.danmaku.token,
  99. user: userId,
  100. bottom: '15%',
  101. unlimited: true
  102. }
  103. })
  104. // 设置音量
  105. //player.volume(0.1, true, false)
  106. // 跳转到上次看到的位置
  107. player.seek(pos)
  108. /* 事件绑定 */
  109. player.on('progress', function() {
  110. // SocketInstance.send({ videoId: videoId, currentTime: player.video.currentTime })
  111. })
  112. player.on('ended', () => {
  113. // SocketInstance.send({ videoId: videoId, currentTime: player.video.currentTime })
  114. const path = this.$route.path
  115. const nextPath = '/video/gz5RYkw1zn'
  116. if (path !== nextPath) {
  117. this.$router.push(nextPath)
  118. } else {
  119. console.log('视频播放完成')
  120. }
  121. })
  122. player.on('volumechange', () => {
  123. console.log('声音改变')
  124. })
  125. },
  126. initFlvPlayer(videoId, coverUrl, videoUrl) {
  127. const dp = new DPlayer({
  128. container: document.getElementById('dplayer'),
  129. live: true,
  130. danmaku: false,
  131. video: {
  132. url: videoUrl,
  133. type: 'customFlv',
  134. customType: {
  135. customFlv: function(video, player) {
  136. const flvPlayer = flvjs.createPlayer({
  137. type: 'flv',
  138. url: video.src
  139. })
  140. flvPlayer.attachMediaElement(video)
  141. flvPlayer.load()
  142. }
  143. }
  144. }
  145. })
  146. }
  147. }
  148. }
  149. </script>
  150. <style>
  151. </style>