videoPlay.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. import { nextTick } from "vue";
  14. export default {
  15. name: "videoPlay",
  16. data() {
  17. return {
  18. playAuth: "", //播放凭证
  19. hideFlag: 'show',
  20. videoId: "", //阿里云视频id
  21. seekTime: '',
  22. playCount: 0
  23. };
  24. },
  25. onUnload() {
  26. // 切换
  27. this.hideFlag = 'hide'
  28. },
  29. onHide() {
  30. this.hideFlag = 'hide'
  31. },
  32. methods: {
  33. init(options) {
  34. this.videoId = options.videoId;
  35. this.seekTime = options.seekTime;
  36. this.setSeekTime(options.seekTime)
  37. const isPlay = options.isPlay || false;
  38. setTimeout(() => {
  39. this.playAuth = options.playAuth; // 最后赋值
  40. if(isPlay) {
  41. this.playCount++
  42. }
  43. }, 300)
  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. console.log('ccccxx',this.videoId)
  112. console.log('xxxx',this.playAuth)
  113. let player = new Aliplayer({
  114. id: "wgy-player-test",
  115. vid: this.videoId,
  116. playauth: this.playAuth,
  117. extraInfo: {
  118. poster: 'noposter'
  119. },
  120. skinLayout: [{
  121. name: "bigPlayButton",
  122. align: "blabs",
  123. x: 30,
  124. y: 80
  125. },
  126. {
  127. name: "H5Loading",
  128. align: "cc"
  129. },
  130. {
  131. name: "controlBar",
  132. align: "blabs",
  133. x: 0,
  134. y: 0,
  135. children: [{
  136. name: "progress",
  137. align: "blabs",
  138. x: 0,
  139. y: 44
  140. },
  141. {
  142. name: "playButton",
  143. align: "tl",
  144. x: 15,
  145. y: 12
  146. },
  147. // {
  148. // name: "fullScreenButton",
  149. // align: "tr",
  150. // x: 10,
  151. // y: 12
  152. // },
  153. {
  154. name: "timeDisplay",
  155. align: "tr",
  156. x: 10,
  157. y: 5
  158. }
  159. ]
  160. }
  161. ],
  162. qualitySort: "asc",
  163. format: "mp4",
  164. mediaType: "video",
  165. encryptType: 1,
  166. autoplay: false,
  167. isLive: false,
  168. rePlay: false,
  169. playsinline: true,
  170. preload: false,
  171. controlBarVisibility: "hover",
  172. useH5Prism: true
  173. }, function(player) {
  174. console.log('ppp', player)
  175. }, (err) => {
  176. console.log('ccccxxxzzzzaa', err)
  177. });
  178. this.player = player;
  179. player.on('canplay', function() {
  180. player.tag.play();
  181. });
  182. player.on('ended', function() {
  183. that.$ownerInstance.callMethod('playEnd', {
  184. data: 'end'
  185. })
  186. });
  187. player.on('pause', function() {
  188. that.$ownerInstance.callMethod('handlePause')
  189. });
  190. player.on('play', function() {
  191. that.$ownerInstance.callMethod('handlePlay')
  192. });
  193. player.on('timeupdate', function(time) {
  194. that.$ownerInstance.callMethod('handleTimeupdate',time.target.childNodes[0].currentTime)
  195. });
  196. },
  197. isHasScript() {
  198. let scriptExists = false;
  199. for (let script of document.scripts) {
  200. if (script.src === 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js') {
  201. scriptExists = true;
  202. break;
  203. }
  204. }
  205. return scriptExists
  206. },
  207. loadWebPlayerSDK() {
  208. if (this.player) {
  209. this.player.dispose();
  210. }
  211. if (this.isHasScript()) {
  212. this.playAli()
  213. return;
  214. }
  215. return new Promise((resolve, reject) => {
  216. const s_tag = document.createElement('script'); // 引入播放器js
  217. s_tag.type = 'text/javascript';
  218. s_tag.src = 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js';
  219. s_tag.charset = 'utf-8';
  220. s_tag.onload = () => {
  221. // console.log(this.playAuth);
  222. setTimeout(() => {
  223. this.playAli()
  224. }, 10);
  225. resolve();
  226. }
  227. document.body.appendChild(s_tag);
  228. const l_tag = document.createElement('link'); // 引入播放器css
  229. l_tag.rel = 'stylesheet';
  230. l_tag.href =
  231. 'https://g.alicdn.com/de/prismplayer/2.9.6/skins/default/aliplayer-min.css';
  232. document.body.appendChild(l_tag);
  233. });
  234. },
  235. }
  236. }
  237. </script>
  238. <style lang="scss">
  239. </style>