VideoPlayer.vue 4.1 KB

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