LivePlayer.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div id="dplayer" ref="dplayer" style="height: 480px;" />
  3. </template>
  4. <script>
  5. import flvjs from 'flv.js'
  6. import DPlayer from 'dplayer'
  7. import { getCamPullUrl } from '@/api/cam'
  8. export default {
  9. name: 'LivePlayer',
  10. props: {
  11. videoProp: {
  12. type: Object,
  13. default: () => null
  14. }
  15. },
  16. data() {
  17. return {
  18. flvjs,
  19. DPlayer,
  20. getUrl: true
  21. }
  22. },
  23. created() {
  24. },
  25. mounted() {
  26. const camId = this.videoProp.videoId
  27. this.getPullUrl(camId)
  28. },
  29. methods: {
  30. getPullUrl(camId) {
  31. getCamPullUrl(camId).then(resp => {
  32. if (resp.code === 0) {
  33. this.initFlvPlayer(resp.data)
  34. } else {
  35. this.$notify.error({
  36. message: '获取摄像头拉流地址失败',
  37. type: 'warning',
  38. duration: 3000
  39. })
  40. }
  41. }).catch(error => {
  42. this.$notify.error({
  43. message: error.message,
  44. type: 'warning',
  45. duration: 3000
  46. })
  47. })
  48. },
  49. initFlvPlayer(videoUrl) {
  50. new DPlayer({
  51. container: document.getElementById('dplayer'),
  52. live: true,
  53. video: {
  54. url: videoUrl,
  55. type: 'customFlv',
  56. customType: {
  57. customFlv: function(video, player) {
  58. const flvPlayer = flvjs.createPlayer({
  59. type: 'flv',
  60. url: video.src
  61. })
  62. flvPlayer.attachMediaElement(video)
  63. flvPlayer.load()
  64. }
  65. }
  66. }
  67. })
  68. }
  69. }
  70. }
  71. </script>
  72. <style>
  73. </style>