h5Player2.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="player" ref="player">
  3. <canvas :id="videoId"></canvas>
  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 wasmUrl = getImages('/assetsMobile/script/WXInlinePlayer/prod.all.wasm.combine.js')
  12. let asmUrl = getImages('/assetsMobile/script/WXInlinePlayer/prod.all.asm.combine.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. let el = document.getElementById(`${this.videoId}`).firstChild
  39. this.createPlayer(el, newVal);
  40. })
  41. }
  42. }
  43. }
  44. },
  45. components: {
  46. },
  47. created() {
  48. },
  49. data() {
  50. return {
  51. flv: null,
  52. videoId: uniqueId('flv_video_'),
  53. tips: '加载中',
  54. }
  55. },
  56. methods: {
  57. createCanvas() {
  58. console.log(this.$refs,'refsresr')
  59. let canvas = document.createElement('canvas')
  60. canvas.setAttribute('id',this.videoId)
  61. this.$refs.player.$el.appendChild(canvas)
  62. },
  63. //创建播放器
  64. createPlayer(el,url) {
  65. // this.createCanvas()
  66. if (window.WXInlinePlayer.isSupport()) {
  67. console.log(el,'elel')
  68. window.WXInlinePlayer.init({
  69. asmUrl: asmUrl,
  70. wasmUrl: wasmUrl,
  71. })
  72. WXInlinePlayer.ready().then(() => {
  73. this.flv = new WXInlinePlayer({
  74. url: url,
  75. $container: el,
  76. hasVideo: true,
  77. hasAudio: true,
  78. volume: 1.0,
  79. muted: false,
  80. autoplay: true,
  81. loop: true,
  82. isLive: true,
  83. chunkSize: 128 * 1024,
  84. preloadTime: 5e2,
  85. bufferingTime: 1e3,
  86. cacheSegmentCount: 64,
  87. customLoader: null
  88. /* hasVideo: true,
  89. hasAudio: true,
  90. volume: 1.0,
  91. muted: false,
  92. autoplay: true,
  93. loop: false,
  94. isLive: true,
  95. chunkSize: 128 * 1024,
  96. preloadTime: 500,
  97. bufferingTime: 500,
  98. cacheSegmentCount: 64,
  99. customLoader: null, */
  100. });
  101. this.flv.play();
  102. this.flv.on("loadError", (e) => {
  103. console.e("loadError", e);
  104. });
  105. this.flv.on("loadSuccess", () => {
  106. console.log("loadSuccess");
  107. });
  108. /* const { userAgent } = navigator;
  109. const isWeChat = /MicroMessenger/i.test(userAgent);
  110. if (!isWeChat) {
  111. alert('click to play!');
  112. document.body.addEventListener('click', () => {
  113. player.play();
  114. });
  115. } */
  116. });
  117. /* if (!this.flv) {
  118. this.flv = new window.WXInlinePlayer({
  119. container: el,
  120. url,
  121. hasVideo: true,
  122. hasAudio: true,
  123. volume: 1.0,
  124. muted: false,
  125. autoplay: true,
  126. loop: false,
  127. isLive: true,
  128. chunkSize: 128 * 1024,
  129. preloadTime: 500,
  130. bufferingTime: 500,
  131. cacheSegmentCount: 64,
  132. customLoader: null,
  133. })
  134. } */
  135. }
  136. },
  137. play() {
  138. if(this.flv) this.flv.play()
  139. },
  140. pause() {
  141. if(this.flv) this.flv.pause()
  142. },
  143. //销毁播放方法
  144. playerDestroy() {
  145. if (this.flv) {
  146. this.flv.pause && this.flv.pause();
  147. this.flv.destroy && this.flv.destroy();
  148. this.flv = null;
  149. }
  150. },
  151. },
  152. beforeDestroy() {
  153. this.playerDestroy();
  154. },
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. .player {
  159. width: 100%;
  160. height: 100%;
  161. overflow: hidden;
  162. position: relative;
  163. .empty {
  164. display: flex;
  165. justify-content: center;
  166. align-items: center;
  167. font-size: 24rpx;
  168. color: #fff;
  169. width: 100%;
  170. height: 100%;
  171. }
  172. }
  173. </style>