videoPlay.vue 4.2 KB

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