player.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div id="dplayer" ref="dplayer" />
  3. </template>
  4. <script>
  5. import { videoUrl } from '@/api/media/video'
  6. import DPlayer from 'dplayer'
  7. const flv = require('flv.js')
  8. const hls = require('hls.js')
  9. export default {
  10. name: 'Play',
  11. data() {
  12. return {
  13. flv,
  14. hls,
  15. videoId: ''
  16. }
  17. },
  18. mounted() {
  19. const userInfo = this.$store.state.userInfo
  20. if (userInfo != null) {
  21. this.userId = userInfo.id.toString()
  22. } else {
  23. this.userId = 111222333
  24. }
  25. this.videoId = this.$route.params.id
  26. this.getVideoUrl(this.videoId)
  27. },
  28. methods: {
  29. // TODO 获取弹幕配置,将 videoUrl 作为本函数的回调
  30. danmakuConfig() {
  31. },
  32. /* videoUrl() {
  33. fetch(`/api/media/video/url/${this.videoId}`, {
  34. headers: {
  35. 'Content-Type': 'application/json; charset=UTF-8',
  36. 'X-XSRF-TOKEN': this.$cookies.get('XSRF-TOKEN')
  37. },
  38. method: 'GET',
  39. credentials: 'include'
  40. }).then(response => response.json())
  41. .then(json => {
  42. if (json.code === 0) {
  43. var coverUrl = json.data.coverUrl
  44. var videoUrl = json.data.videoUrl
  45. this.initDplayer(this.videoId, coverUrl, videoUrl)
  46. } else {
  47. // TODO 显示错误信息
  48. // this.$router.push('/')
  49. }
  50. })
  51. .catch(e => {
  52. return null
  53. })
  54. },*/
  55. getVideoUrl(videoId) {
  56. videoUrl(videoId)
  57. .then(res => {
  58. if (res.code === 0) {
  59. // TODO 返回一个 dplayer 播放器对象,包含一些常用的属性
  60. var coverUrl = res.data.coverUrl
  61. var videoUrl = res.data.videoUrl
  62. this.initDplayer(this.videoId, coverUrl, videoUrl)
  63. } else {
  64. this.$notify({
  65. title: res.code,
  66. message: res.msg,
  67. type: 'warning',
  68. duration: 500
  69. })
  70. }
  71. })
  72. .catch(error => {
  73. this.$message.error(error.message)
  74. })
  75. },
  76. initDplayer(videoId, coverUrl, videoUrl) {
  77. const vidId = videoId
  78. const dp = new DPlayer({
  79. container: document.querySelector('#dplayer'),
  80. lang: 'zh-cn',
  81. autoplay: false,
  82. screenshot: true,
  83. video: {
  84. pic: coverUrl,
  85. url: videoUrl,
  86. type: 'auto'
  87. },
  88. logo: '/logo.png',
  89. danmaku: {
  90. id: videoId,
  91. maximum: 10000,
  92. api: '/api/media/danmaku/',
  93. token: 'hertube',
  94. user: this.userId,
  95. bottom: '15%',
  96. unlimited: true
  97. }
  98. })
  99. // 跳转到指定秒
  100. // dp.seek(100)
  101. dp.on('play', function() {
  102. console.log(vidId + ' 播放开始' + dp.video.currentTime)
  103. })
  104. dp.on('pause', function() {
  105. console.log(vidId + ' 播放暂停' + dp.video.currentTime)
  106. })
  107. dp.on('ended', function() {
  108. // TODO 当前视频播放完成后判断是否继续播放相关推荐中的视频
  109. console.log(vidId + ' 播放结束' + dp.video.currentTime)
  110. })
  111. dp.on('seeking', function() {
  112. console.log(vidId + ' seeking 事件 ' + dp.video.currentTime)
  113. })
  114. dp.on('seeked', function() {
  115. console.log(vidId + ' seeked 事件' + dp.video.currentTime)
  116. })
  117. const interval = 5
  118. var i = 0
  119. dp.on('progress', function() {
  120. console.log('i = ' + i)
  121. if (i % interval === 0) {
  122. // TODO 每 5s 向后端报告一次播放进度
  123. console.log(vidId + ' 播放进度 ' + dp.video.currentTime)
  124. }
  125. i++
  126. })
  127. }
  128. }
  129. }
  130. </script>
  131. <style>
  132. #dplayer {
  133. height: 500px;
  134. }
  135. </style>