AliPlay.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <template>
  2. <div>
  3. <div class="prism-player" :id="playerId" :style="playStyle"></div>
  4. </div>
  5. </template>
  6. <script>
  7. // import '../api/aliplayer-h5-min'
  8. // import '../api/aliplayer-min'
  9. export default {
  10. name: "Aliplayer",
  11. props: {
  12. playStyle: {
  13. type: String,
  14. default: ""
  15. },
  16. aliplayerSdkPath: {
  17. // Aliplayer 代码的路径
  18. type: String,
  19. default: "//g.alicdn.com/de/prismplayer/2.8.2/aliplayer-min.js"
  20. },
  21. autoplay: {
  22. type: Boolean,
  23. default: false
  24. },
  25. isLive: {
  26. type: Boolean,
  27. default: false
  28. },
  29. playsinline: { // H5是否内置播放,有的Android浏览器不起作用。
  30. type: Boolean,
  31. default: false
  32. },
  33. width: {
  34. type: String,
  35. default: "100%"
  36. },
  37. height: {
  38. type: String,
  39. default: "320px"
  40. },
  41. controlBarVisibility: { // 控制面板的实现,默认为‘hover’, 可选的值为:‘click’、‘hover’、‘always’。
  42. type: String,
  43. default: "hover"
  44. },
  45. useH5Prism: {
  46. type: Boolean,
  47. default: false
  48. },
  49. useFlashPrism: {
  50. type: Boolean,
  51. default: false
  52. },
  53. vid: {
  54. type: String,
  55. default: ""
  56. },
  57. playauth: {
  58. type: String,
  59. default: ""
  60. },
  61. source: {
  62. type: String,
  63. default: "rtmp://test.noichina.com/app1/stream1?auth_key=1568256446-0-0-4b22ba4312c1f6343f4f8994a6f51cdd"
  64. },
  65. cover: {
  66. type: String,
  67. default: ""
  68. },
  69. format: {
  70. type: String,
  71. default: "m3u8"
  72. },
  73. skinLayout: {
  74. type: Array,
  75. default: function () {
  76. return [];
  77. }
  78. },
  79. x5_video_position: {
  80. type: String,
  81. default: "top"
  82. },
  83. x5_type: {
  84. type: String,
  85. default: "h5"
  86. },
  87. x5_fullscreen: {
  88. type: Boolean,
  89. default: false
  90. },
  91. x5_orientation: {
  92. type: Number,
  93. default: 2
  94. },
  95. autoPlayDelay: {
  96. type: Number,
  97. default: 0
  98. },
  99. autoPlayDelayDisplayText: {
  100. type: String
  101. }
  102. },
  103. data() {
  104. return {
  105. playerId: "aliplayer_" + Math.random().toString(36).substr(2),
  106. scriptTagStatus: 0,
  107. isReload: false,
  108. instance: null
  109. }
  110. },
  111. created() {
  112. if (window.Aliplayer !== undefined) {
  113. // 如果全局对象存在,说明编辑器代码已经初始化完成,直接加载编辑器
  114. this.scriptTagStatus = 2;
  115. this.initAliplayer();
  116. } else {
  117. // 如果全局对象不存在,说明编辑器代码还没有加载完成,需要加载编辑器代码
  118. this.insertScriptTag();
  119. }
  120. },
  121. mounted() {
  122. if (window.Aliplayer !== undefined) {
  123. // 如果全局对象存在,说明编辑器代码已经初始化完成,直接加载编辑器
  124. this.scriptTagStatus = 2;
  125. this.initAliplayer();
  126. } else {
  127. // 如果全局对象不存在,说明编辑器代码还没有加载完成,需要加载编辑器代码
  128. this.insertScriptTag();
  129. }
  130. },
  131. methods: {
  132. insertScriptTag() {
  133. const _this = this;
  134. let playerScriptTag = document.getElementById("playerScriptTag");
  135. // 如果这个tag不存在,则生成相关代码tag以加载代码
  136. if (playerScriptTag === null) {
  137. playerScriptTag = document.createElement("script");
  138. playerScriptTag.type = "text/javascript";
  139. playerScriptTag.src = this.aliplayerSdkPath;
  140. playerScriptTag.id = "playerScriptTag";
  141. let s = document.getElementsByTagName("head")[0];
  142. s.appendChild(playerScriptTag);
  143. }
  144. if (playerScriptTag.loaded) {
  145. _this.scriptTagStatus++;
  146. } else {
  147. playerScriptTag.addEventListener("load", () => {
  148. _this.scriptTagStatus++;
  149. playerScriptTag.loaded = true;
  150. _this.initAliplayer();
  151. });
  152. }
  153. _this.initAliplayer();
  154. },
  155. initAliplayer() {
  156. const _this = this;
  157. // scriptTagStatus 为 2 的时候,说明两个必需引入的 js 文件都已经被引入,且加载完成
  158. if (_this.scriptTagStatus === 2 && (_this.instance === null || _this.reloadPlayer)) {
  159. _this.instance && _this.instance.dispose();
  160. // document.querySelector("#" + _this.playerId).innerHTML = "";
  161. // Vue 异步执行 DOM 更新,这样一来代码执行到这里的时候可能 template 里面的 script 标签还没真正创建
  162. // 所以,我们只能在 nextTick 里面初始化 Aliplayer
  163. _this.$nextTick(() => {
  164. _this.instance = window.Aliplayer({
  165. id: _this.playerId,
  166. autoplay: _this.autoplay,
  167. isLive: _this.isLive,
  168. playsinline: _this.playsinline,
  169. format: _this.format,
  170. width: _this.width,
  171. height: _this.height,
  172. controlBarVisibility: _this.controlBarVisibility,
  173. useH5Prism: _this.useH5Prism,
  174. useFlashPrism: _this.useFlashPrism,
  175. vid: _this.vid,
  176. playauth: _this.playauth,
  177. source: _this.source,
  178. cover: _this.cover,
  179. // skinLayout: _this.skinLayout, // 说明:功能组件布局配置,不传该字段使用默认布局传false隐藏所有功能组件,请参照皮肤定制
  180. x5_video_position: _this.x5_video_position,
  181. x5_type: _this.x5_type,
  182. x5_fullscreen: _this.x5_fullscreen,
  183. x5_orientation: _this.x5_orientation,
  184. autoPlayDelay: _this.autoPlayDelay,
  185. autoPlayDelayDisplayText: _this.autoPlayDelayDisplayText
  186. });
  187. // 绑定事件,当 AliPlayer 初始化完成后,将编辑器实例通过自定义的 ready 事件交出去
  188. _this.instance.on("ready", () => {
  189. this.$emit("ready", _this.instance);
  190. });
  191. _this.instance.on("play", () => {
  192. this.$emit("play", _this.instance);
  193. });
  194. _this.instance.on("pause", () => {
  195. this.$emit("pause", _this.instance);
  196. });
  197. _this.instance.on("ended", () => {
  198. this.$emit("ended", _this.instance);
  199. });
  200. _this.instance.on("liveStreamStop", () => {
  201. this.$emit("liveStreamStop", _this.instance);
  202. });
  203. _this.instance.on("m3u8Retry", () => {
  204. this.$emit("m3u8Retry", _this.instance);
  205. });
  206. _this.instance.on("hideBar", () => {
  207. this.$emit("hideBar", _this.instance);
  208. });
  209. _this.instance.on("waiting", () => {
  210. this.$emit("waiting", _this.instance);
  211. });
  212. _this.instance.on("snapshoted", () => {
  213. this.$emit("snapshoted", _this.instance);
  214. });
  215. _this.instance.on("timeupdate", () => {
  216. _this.$emit("timeupdate", _this.instance);
  217. });
  218. _this.instance.on("requestFullScreen", () => {
  219. _this.$emit("requestFullScreen", _this.instance);
  220. });
  221. _this.instance.on("cancelFullScreen", () => {
  222. _this.$emit("cancelFullScreen", _this.instance);
  223. });
  224. _this.instance.on("error", () => {
  225. _this.$emit("error", _this.instance);
  226. });
  227. _this.instance.on("startSeek", () => {
  228. _this.$emit("startSeek", _this.instance);
  229. });
  230. _this.instance.on("completeSeek", () => {
  231. _this.$emit("completeSeek", _this.instance);
  232. });
  233. });
  234. }
  235. },
  236. /**
  237. * 播放视频
  238. */
  239. play: function () {
  240. this.instance.play();
  241. },
  242. /**
  243. * 暂停视频
  244. */
  245. pause: function () {
  246. this.instance.pause();
  247. },
  248. /**
  249. * 重播视频
  250. */
  251. replay: function () {
  252. this.instance.replay();
  253. },
  254. /**
  255. * 跳转到某个时刻进行播放
  256. * @argument time 的单位为秒
  257. */
  258. seek: function (time) {
  259. this.instance.seek(time);
  260. },
  261. /**
  262. * 获取当前时间 单位秒
  263. */
  264. getCurrentTime: function () {
  265. return this.instance.getCurrentTime();
  266. },
  267. /**
  268. *获取视频总时长,返回的单位为秒
  269. * @returns 返回的单位为秒
  270. */
  271. getDuration: function () {
  272. return this.instance.getDuration();
  273. },
  274. /**
  275. 获取当前的音量,返回值为0-1的实数ios和部分android会失效
  276. */
  277. getVolume: function () {
  278. return this.instance.getVolume();
  279. },
  280. /**
  281. 设置音量,vol为0-1的实数,ios和部分android会失效
  282. */
  283. setVolume: function (vol) {
  284. this.instance.setVolume(vol);
  285. },
  286. /**
  287. *直接播放视频url,time为可选值(单位秒)目前只支持同种格式(mp4/flv/m3u8)之间切换暂不支持直播rtmp流切换
  288. *@argument url 视频地址
  289. *@argument time 跳转到多少秒
  290. */
  291. loadByUrl: function (url, time) {
  292. this.instance.loadByUrl(url, time);
  293. },
  294. /**
  295. * 设置播放速度
  296. *@argument speed 速度
  297. */
  298. setSpeed: function (speed) {
  299. this.instance.setSpeed(speed);
  300. },
  301. /**
  302. * 设置播放器大小w,h可分别为400px像素或60%百分比chrome浏览器下flash播放器分别不能小于397x297
  303. *@argument w 播放器宽度
  304. *@argument h 播放器高度
  305. */
  306. setPlayerSize: function (w, h) {
  307. this.instance.setPlayerSize(w, h);
  308. },
  309. /**
  310. * 目前只支持HTML5界面上的重载功能,暂不支持直播rtmp流切换m3u8)之间切换,暂不支持直播rtmp流切换
  311. *@argument vid 视频id
  312. *@argument playauth 播放凭证
  313. */
  314. reloaduserPlayInfoAndVidRequestMts: function (vid, playauth) {
  315. this.instance.reloaduserPlayInfoAndVidRequestMts(vid, playauth);
  316. },
  317. reloadPlayer: function () {
  318. this.isReload = true;
  319. this.initAliplayer();
  320. this.isReload = false;
  321. },
  322. },
  323. }
  324. </script>
  325. <style lang="postcss" scoped>
  326. @import 'https://g.alicdn.com/de/prismplayer/2.8.2/skins/default/aliplayer-min.css';
  327. </style>