videoPlay.vue 5.7 KB

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