videoPlay.vue 5.2 KB

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