h5Player.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view class="player">
  3. <div :id="videoId"></div>
  4. <view class="empty">{{tips}}</view>
  5. </view>
  6. </template>
  7. <script>
  8. /* https://www.npmjs.com/package/flvplayer */
  9. import {getImages} from '@/plugins/images'
  10. import { uniqueId } from 'lodash'
  11. let multipleDecoderJs = getImages('/assetsMobile/script/flvPlayer/flvplayer-decoder-multiple.js')
  12. let baselineDecoderJs = getImages('/assetsMobile/script/flvPlayer/flvplayer-decoder-baseline.js')
  13. export default {
  14. props: {
  15. videoUrl: {
  16. type: String,
  17. default: ''
  18. },
  19. control: {
  20. type: Boolean,
  21. default: true,
  22. }
  23. },
  24. watch: {
  25. videoUrl: {
  26. immediate: true,
  27. deep: true,
  28. handler(newVal,oldVal) {
  29. this.tips = '加载中'
  30. // console.log(newVal,'vv')
  31. if (!this.isNull(newVal)) {
  32. if(newVal.includes('failed')) {
  33. this.tips = '设备命令下发失败'
  34. return
  35. }
  36. this.playerDestroy();
  37. this.$nextTick(() => {
  38. this.createPlayer(`#${this.videoId}`, newVal);
  39. })
  40. }
  41. }
  42. }
  43. },
  44. components: {
  45. },
  46. created() {
  47. },
  48. data() {
  49. return {
  50. flv: null,
  51. videoId: uniqueId('flv_video_'),
  52. tips: '加载中',
  53. }
  54. },
  55. methods: {
  56. //创建播放器
  57. createPlayer(el,url) {
  58. if (!this.flv) {
  59. this.flv = new window.FlvPlayer({
  60. container: el,
  61. url,
  62. decoder: multipleDecoderJs,
  63. cache: false,
  64. muted: true,
  65. videoChunk: 128 * 128,
  66. live: true,
  67. autoPlay: true,
  68. hasAudio: false,
  69. debug: true,
  70. control: this.control
  71. })
  72. }
  73. },
  74. play() {
  75. if(this.flv) this.flv.play()
  76. },
  77. pause() {
  78. if(this.flv) this.flv.pause()
  79. },
  80. //销毁播放方法
  81. playerDestroy() {
  82. if (this.flv) {
  83. this.flv.pause && this.flv.pause();
  84. this.flv.destroy && this.flv.destroy();
  85. this.flv = null;
  86. }
  87. },
  88. },
  89. beforeDestroy() {
  90. this.playerDestroy();
  91. },
  92. }
  93. </script>
  94. <style lang="scss" scoped>
  95. .player {
  96. width: 100%;
  97. height: 100%;
  98. overflow: hidden;
  99. position: relative;
  100. .empty {
  101. display: flex;
  102. justify-content: center;
  103. align-items: center;
  104. font-size: 24rpx;
  105. color: #fff;
  106. width: 100%;
  107. height: 100%;
  108. }
  109. }
  110. </style>