videoPlay.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view class="ezy-video-box course-video-box">
  3. <view ref="videoContent" id="video-play1" :playAuth="playAuth1"
  4. :change:playAuth="renderScript.receiveMsg" :videoId="videoId1" :change:videoId="renderScript.videoIdFun"
  5. :hideFlag="hideFlag1" :change:hideFlag="renderScript.hideFlagFun" :progressMarkers="progressMarkers1"
  6. :change:progressMarkers="renderScript.progressMarkersMsg" :seekTime="seekTime1"
  7. :change:seekTime="renderScript.seekTimeFun" class="ezy-video">
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. import {MyRenderEventBus} from "./renderEvents"
  13. export default {
  14. name: "videoPlay",
  15. props: {
  16. cId: {
  17. type: String,
  18. },
  19. progressMarkers1: {
  20. type: Array,
  21. default: () => ([])
  22. },
  23. videoId1: {
  24. type: String,
  25. },
  26. playAuth1: {
  27. type: String,
  28. },
  29. seekTime1: {
  30. type: String
  31. },
  32. hideFlag1: {
  33. type: String
  34. }
  35. },
  36. data() {
  37. return {}
  38. },
  39. methods: {
  40. handleStop() {
  41. MyRenderEventBus.$emit('video-play1')
  42. }
  43. }
  44. }
  45. </script>
  46. <script module="renderScript" lang="renderjs">
  47. import {MyRenderEventBus} from "./renderEvents";
  48. export default {
  49. mounted() {
  50. MyRenderEventBus.$on(`video-play1`, this.stopPlayer)
  51. },
  52. Unmounted() {
  53. MyRenderEventBus.$ff(`video-play1`, this.stopPlayer)
  54. },
  55. data() {
  56. return {
  57. player: null,
  58. playAuth: '',
  59. videoId: '',
  60. progressMarkers: [],
  61. isFullScreen: false,
  62. seekTime: '',
  63. }
  64. },
  65. methods: {
  66. // 通用的方法调用器
  67. handleRenderMethod(methodName, params) {
  68. if (this[methodName] && typeof this[methodName] === 'function') {
  69. return this[methodName](params)
  70. }
  71. },
  72. stopPlayer() {
  73. this.player && this.player.pause();
  74. },
  75. receiveMsg(newValue, oldValue, ownerInstance, instance) {
  76. if (newValue) {
  77. this.playAuth = ''
  78. this.playAuth = newValue
  79. this.loadWebPlayerSDK()
  80. }
  81. },
  82. videoIdFun(newValue, oldValue, ownerInstance, instance) {
  83. if (newValue) {
  84. this.videoId = ''
  85. this.videoId = newValue
  86. }
  87. },
  88. hideFlagFun(newValue, oldValue, ownerInstance, instance) {
  89. if (this.player) {
  90. this.player.pause()
  91. }
  92. },
  93. progressMarkersMsg(newValue, oldValue, ownerInstance, instance) {
  94. if (newValue) {
  95. this.progressMarkers = newValue
  96. }
  97. },
  98. seekTimeFun(newValue, oldValue, ownerInstance, instance) {
  99. if (newValue) {
  100. this.player.play()
  101. this.player.seek(newValue)
  102. }
  103. },
  104. playAli() {
  105. let that = this
  106. //配置播放器
  107. if (!this.playAuth) {
  108. return false;
  109. }
  110. var player = new Aliplayer({
  111. id: 'video-play1',
  112. "vid": this.videoId,
  113. "playauth": this.playAuth,
  114. extraInfo: {
  115. poster: 'noposter'
  116. },
  117. fullscreenEvents: {
  118. fullscreenChange: (isFull) => {
  119. this.isFullScreen = isFull
  120. }
  121. },
  122. "skinLayout": [{
  123. "name": "bigPlayButton",
  124. "align": "blabs",
  125. "x": 30,
  126. "y": 80
  127. },
  128. {
  129. "name": "H5Loading",
  130. "align": "cc"
  131. },
  132. {
  133. "name": "controlBar",
  134. "align": "blabs",
  135. "x": 0,
  136. "y": 0,
  137. "children": [{
  138. "name": "progress",
  139. "align": "blabs",
  140. "x": 0,
  141. "y": 44
  142. },
  143. {
  144. "name": "playButton",
  145. "align": "tl",
  146. "x": 15,
  147. "y": 12
  148. },
  149. {
  150. "name": "fullScreenButton",
  151. "align": "tr",
  152. "x": 10,
  153. "y": 12
  154. },
  155. {
  156. "name": "timeDisplay",
  157. "align": "tr",
  158. "x": 10,
  159. "y": 5
  160. }
  161. ]
  162. }
  163. ],
  164. "qualitySort": "asc",
  165. "format": "mp4",
  166. "mediaType": "video",
  167. "encryptType": 1,
  168. "progressMarkers": this.progressMarkers,
  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. this.player = player;
  178. player.on('canplay', function() {
  179. player.tag.play();
  180. });
  181. player.on('ended', function(data) {
  182. that.exitFullScreen();
  183. that.$ownerInstance.callMethod('playEnd', {
  184. data: 'end'
  185. })
  186. });
  187. },
  188. exitFullScreen() {
  189. if (document.exitFullscreen) {
  190. document.exitFullscreen(); // 标准方法
  191. } else if (document.mozCancelFullScreen) { // Firefox
  192. document.mozCancelFullScreen();
  193. } else if (document.webkitExitFullscreen) { // Chrome, Safari & Opera
  194. document.webkitExitFullscreen();
  195. } else if (document.msExitFullscreen) { // IE/Edge
  196. document.msExitFullscreen();
  197. }
  198. },
  199. loadWebPlayerSDK() {
  200. return new Promise((resolve, reject) => {
  201. const s_tag = document.createElement('script'); // 引入播放器js
  202. s_tag.type = 'text/javascript';
  203. s_tag.src = 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js';
  204. s_tag.charset = 'utf-8';
  205. s_tag.onload = () => {
  206. // console.log(this.playAuth);
  207. this.playAli()
  208. resolve();
  209. }
  210. document.body.appendChild(s_tag);
  211. const l_tag = document.createElement('link'); // 引入播放器css
  212. l_tag.rel = 'stylesheet';
  213. l_tag.href =
  214. 'https://g.alicdn.com/de/prismplayer/2.9.6/skins/default/aliplayer-min.css';
  215. document.body.appendChild(l_tag);
  216. });
  217. },
  218. }
  219. }
  220. </script>