videoPlay.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view>
  3. <view ref="videoContent" id="wgy-player-test" :playAuth="playAuth" :change:playAuth="renderScript.receiveMsg"
  4. :videoId="videoId" :change:videoId="renderScript.videoIdFun"
  5. :playCount="playCount" :change:playCount="renderScript.playVideo"
  6. :status="hideFlag"
  7. :change:hideFlag="renderScript.hideFlagFun" :seekTime="seekTime" :change:seekTime="renderScript.seekTimeFun"
  8. class="kecheng-video">
  9. <zmm-watermark :watermark="watermark"></zmm-watermark>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import cacheManager from "@/utils/cacheManager.js";
  15. export default {
  16. name: "videoPlay",
  17. data() {
  18. return {
  19. playAuth: "", //播放凭证
  20. hideFlag: 'show',
  21. videoId: "", //阿里云视频id
  22. seekTime: '',
  23. playCount: 0,
  24. watermark: `<h5>${cacheManager.get("auth").userName}-${cacheManager.get("auth").jzName}"</h5>`
  25. };
  26. },
  27. onUnload() {
  28. // 切换
  29. this.hideFlag = 'hide'
  30. },
  31. onHide() {
  32. this.hideFlag = 'hide'
  33. },
  34. methods: {
  35. init(options) {
  36. this.playAuth = options.playAuth;
  37. this.videoId = options.videoId;
  38. this.seekTime = options.seekTime;
  39. this.setSeekTime(options.seekTime)
  40. const isPlay = options.isPlay || false;
  41. if(isPlay) {
  42. this.playCount++
  43. }
  44. },
  45. setSeekTime(seekTime) {
  46. this.seekTime = '';
  47. this.$nextTick(() => {
  48. this.seekTime = seekTime;
  49. })
  50. },
  51. handlePause() {
  52. this.$emit('play-pause')
  53. },
  54. handlePlay() {
  55. this.$emit('play-play')
  56. },
  57. playEnd() {
  58. this.$emit('play-end')
  59. },
  60. handleTimeupdate(timer) {
  61. this.$emit('play-timeupdate',timer)
  62. }
  63. }
  64. }
  65. </script>
  66. <script module="renderScript" lang="renderjs">
  67. export default {
  68. data() {
  69. return {
  70. player: null,
  71. playAuth: '',
  72. videoId: '',
  73. }
  74. },
  75. methods: {
  76. playVideo() {
  77. setTimeout(() => {
  78. this.player && this.player.play()
  79. }, 300);
  80. },
  81. receiveMsg(newValue, oldValue, ownerInstance, instance) {
  82. if (newValue) {
  83. this.playAuth = ''
  84. this.playAuth = newValue
  85. this.loadWebPlayerSDK()
  86. }
  87. },
  88. videoIdFun(newValue, oldValue, ownerInstance, instance) {
  89. if (newValue) {
  90. this.videoId = ''
  91. this.videoId = newValue
  92. }
  93. },
  94. hideFlagFun(newValue, oldValue, ownerInstance, instance) {
  95. if (this.player) {
  96. this.player.pause()
  97. }
  98. },
  99. seekTimeFun(newValue, oldValue, ownerInstance, instance) {
  100. if (newValue) {
  101. this.player && this.player.seek(newValue)
  102. console.log('已设置时间',newValue)
  103. }
  104. },
  105. playAli() {
  106. let that = this
  107. //配置播放器
  108. if (!this.playAuth) {
  109. return false;
  110. }
  111. let player = new Aliplayer({
  112. id: "wgy-player-test",
  113. vid: this.videoId,
  114. playauth: this.playAuth,
  115. extraInfo: {
  116. poster: 'noposter'
  117. },
  118. skinLayout: [{
  119. name: "bigPlayButton",
  120. align: "blabs",
  121. x: 30,
  122. y: 80
  123. },
  124. {
  125. name: "H5Loading",
  126. align: "cc"
  127. },
  128. {
  129. name: "controlBar",
  130. align: "blabs",
  131. x: 0,
  132. y: 0,
  133. children: [{
  134. name: "progress",
  135. align: "blabs",
  136. x: 0,
  137. y: 44
  138. },
  139. {
  140. name: "playButton",
  141. align: "tl",
  142. x: 15,
  143. y: 12
  144. },
  145. // {
  146. // name: "fullScreenButton",
  147. // align: "tr",
  148. // x: 10,
  149. // y: 12
  150. // },
  151. {
  152. name: "timeDisplay",
  153. align: "tr",
  154. x: 10,
  155. y: 5
  156. }
  157. ]
  158. }
  159. ],
  160. qualitySort: "asc",
  161. format: "mp4",
  162. mediaType: "video",
  163. encryptType: 1,
  164. autoplay: false,
  165. isLive: false,
  166. rePlay: false,
  167. playsinline: true,
  168. preload: false,
  169. controlBarVisibility: "hover",
  170. useH5Prism: true
  171. }, function(player) {
  172. console.log('ppp', player)
  173. });
  174. this.player = player;
  175. player.on('canplay', function() {
  176. player.tag.play();
  177. });
  178. player.on('ended', function() {
  179. that.$ownerInstance.callMethod('playEnd', {
  180. data: 'end'
  181. })
  182. });
  183. player.on('pause', function() {
  184. that.$ownerInstance.callMethod('handlePause')
  185. });
  186. player.on('play', function() {
  187. that.$ownerInstance.callMethod('handlePlay')
  188. });
  189. player.on('timeupdate', function(time) {
  190. that.$ownerInstance.callMethod('handleTimeupdate',time.target.childNodes[0].currentTime)
  191. });
  192. },
  193. isHasScript() {
  194. let scriptExists = false;
  195. for (let script of document.scripts) {
  196. if (script.src === 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js') {
  197. scriptExists = true;
  198. break;
  199. }
  200. }
  201. return scriptExists
  202. },
  203. loadWebPlayerSDK() {
  204. if (this.player) {
  205. this.player.dispose();
  206. }
  207. if (this.isHasScript()) {
  208. this.playAli()
  209. return;
  210. }
  211. return new Promise((resolve, reject) => {
  212. const s_tag = document.createElement('script'); // 引入播放器js
  213. s_tag.type = 'text/javascript';
  214. s_tag.src = 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js';
  215. s_tag.charset = 'utf-8';
  216. s_tag.onload = () => {
  217. // console.log(this.playAuth);
  218. setTimeout(() => {
  219. this.playAli()
  220. }, 10);
  221. resolve();
  222. }
  223. document.body.appendChild(s_tag);
  224. const l_tag = document.createElement('link'); // 引入播放器css
  225. l_tag.rel = 'stylesheet';
  226. l_tag.href =
  227. 'https://g.alicdn.com/de/prismplayer/2.9.6/skins/default/aliplayer-min.css';
  228. document.body.appendChild(l_tag);
  229. });
  230. },
  231. }
  232. }
  233. </script>
  234. <style lang="scss">
  235. </style>