videoPlay.vue 5.1 KB

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