polyvPlayer.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <view>
  3. <view class="player_box" style="width: 100%; height: 421rpx">
  4. <!-- #ifdef MP-WEIXIN -->
  5. <polyv-player
  6. id="playerVideo"
  7. playerId="playerVideo"
  8. height="421rpx"
  9. :vid="vid"
  10. :showSettingBtn="true"
  11. :enablePlayGesture="true"
  12. :custom-cache="false"
  13. :object-fit="'contain'"
  14. @statechange="wxStatechange"
  15. @error="playError"
  16. @timeupdate="timeupdate"
  17. :autoplay="autoplay"
  18. :page-gesture="true"
  19. :vslide-gesture="true"
  20. :vslide-gesture-in-fullscreen="true"
  21. :isAllowSeek="allowSeek"
  22. :playbackRate="playbackRate"
  23. :enableAutoRotation="enableAutoRotation"
  24. @loadedmetadata="loadedmetadata"
  25. ></polyv-player>
  26. <!-- #endif -->
  27. <!-- #ifdef H5 -->
  28. <view id="player"></view>
  29. <!-- #endif -->
  30. <template v-if="videoToastShow">
  31. <cover-view class="video-toast__close" @click="closeToast()"
  32. >X</cover-view
  33. >
  34. <cover-view class="video-toast">
  35. <cover-view class="video-toast__text"
  36. >您上次看到
  37. {{ $method.secondToDate(vct) }},正在自动续播</cover-view
  38. >
  39. <cover-view class="video-toast__btn" @click="restart()"
  40. >从头播放</cover-view
  41. >
  42. </cover-view>
  43. </template>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. var polyvPlayerContext = null;
  49. export default {
  50. name: "SaasMiniprogramPolyvPlayer",
  51. props: {
  52. playVid: {
  53. type: String,
  54. defaule: "",
  55. },
  56. autoplay: {
  57. type: Boolean,
  58. defaule: false,
  59. },
  60. allowSeek: {
  61. type: String,
  62. defaule: () => {
  63. // #ifdef MP-WEIXIN
  64. return "no";
  65. // #endif
  66. // #ifdef H5
  67. return "on";
  68. // #endif
  69. },
  70. },
  71. videoCurrentTime: {
  72. type: Number,
  73. defaule: 0,
  74. },
  75. playbackRate: {
  76. type: Array,
  77. defaule: () => {
  78. return [1.0];
  79. },
  80. },
  81. },
  82. data() {
  83. return {
  84. barTimer: null,
  85. vodPlayerJs: "https://player.polyv.net/resp/vod-player/latest/player.js",
  86. videoToastShow: false,
  87. enableAutoRotation: true,
  88. hasStart: false,
  89. config: null,
  90. Elevideo: null,
  91. };
  92. },
  93. created() {
  94. polyvPlayerContext = null;
  95. },
  96. mounted() {
  97. if (this.vid) {
  98. this.changeVid(this.vid);
  99. }
  100. // if (this.vct > 0) {
  101. // this.videoToastShow = true;
  102. // setTimeout(() => {
  103. // this.closeToast();
  104. // }, 3000);
  105. // }
  106. },
  107. methods: {
  108. loadPlayerScript(callback) {
  109. if (!window.polyvPlayer) {
  110. const myScript = document.createElement("script");
  111. myScript.setAttribute("src", this.vodPlayerJs);
  112. myScript.onload = callback;
  113. document.body.appendChild(myScript);
  114. } else {
  115. callback();
  116. }
  117. },
  118. // 播放视频
  119. loadPlayer() {
  120. const polyvPlayer = window.polyvPlayer;
  121. this.$api.polyvVideoSign(this.vid).then(async (res) => {
  122. let option = {
  123. showLine: "off",
  124. ban_history_time: "on",
  125. vid: this.vid,
  126. forceH5: true,
  127. autoplay: this.autoplay, // 自动播放
  128. ban_seek: this.allowSeek, // 是否禁止拖拽进度条
  129. speed: this.playbackRate, // 倍数
  130. banSeekDeviation: 7, // 做兼容
  131. teaser_show: 1,
  132. tail_show: 1,
  133. hideSwitchPlayer: true,
  134. watchStartTime: this.videoCurrentTime, // 播放开始时间,表示视频从第几秒开始播放,参数值需小于视频时长
  135. ts: res.data.data.ts, // 移动播放加密视频需传入的时间戳。
  136. sign: res.data.data.sign, // 移动端播放加密视频所需的签名
  137. };
  138. if (polyvPlayerContext) {
  139. polyvPlayerContext.changeVid(option);
  140. } else {
  141. option = {
  142. wrap: "#player",
  143. width: "100%",
  144. height: 218,
  145. ...option,
  146. };
  147. polyvPlayerContext = polyvPlayer(option);
  148. this.h5StateChange();
  149. }
  150. });
  151. },
  152. wxStatechange(newstate) {
  153. polyvPlayerContext = this.selectComponent("#playerVideo");
  154. // ["playing", "pause", "ended","error"]
  155. let state = newstate.detail.newstate;
  156. if (state == "error") {
  157. state = "playerError";
  158. }
  159. this[state] && this[state]();
  160. this.$emit(state);
  161. },
  162. playing() {
  163. console.log(polyvPlayerContext, "polyvPlayerContext");
  164. // #ifdef MP-WEIXIN
  165. polyvPlayerContext.seek(this.videoCurrentTime || 0);
  166. // #endif
  167. // #ifdef H5
  168. this.Elevideo = document.querySelector("video.plv-player-video");
  169. this.Elevideo &&
  170. this.Elevideo.addEventListener("timeupdate", this.timeupdate);
  171. // #endif
  172. },
  173. resumeVideo() {
  174. // #ifdef MP-WEIXIN
  175. polyvPlayerContext.play();
  176. // #endif
  177. // #ifdef H5
  178. polyvPlayerContext.j2s_resumeVideo();
  179. // #endif
  180. },
  181. playerError(err) {
  182. console.log("播放err", err);
  183. },
  184. timeupdate(e) {
  185. let time = 0;
  186. // #ifdef MP-WEIXIN
  187. time = e.detail.currentTime;
  188. // #endif
  189. // #ifdef H5
  190. time = this.playCurrentTime();
  191. // #endif
  192. this.$emit("timeupdate", time);
  193. },
  194. h5StateChange() {
  195. let states = {
  196. s2j_onPlayerInitOver: "onPlayerInitOver", // 播放器初始化完毕时触发
  197. s2j_onPlayStart: "onPlayStart", // 视频初次播放时触发
  198. s2j_onVideoPause: "pause", // 视频暂停时触发
  199. s2j_onVideoPlay: "playing", // 视频初次播放或由暂停恢复播放时触发
  200. s2j_onPlayOver: "ended", // 当前视频播放完毕时触发
  201. s2j_onVideoSeek: "onVideoSeek", // 视频拖拽进度时触发
  202. s2j_onPlayerError: "playerError", // 播放出现错误时触发
  203. serverError: "playerError", // 逻辑错误
  204. };
  205. let that = this;
  206. for (const key in states) {
  207. polyvPlayerContext.on(key, function () {
  208. that[states[key]] && that[states[key]](...arguments);
  209. that.$emit(states[key]);
  210. });
  211. }
  212. },
  213. onVideoSeek(start, end, vid) {
  214. polyvPlayerContext.toggleFullscreen();
  215. if (this.allowSeek !== "on") {
  216. return;
  217. }
  218. if (end - start > 10) {
  219. polyvPlayerContext.j2s_seekVideo(start);
  220. }
  221. },
  222. seekVideo(time) {
  223. console.log("🚀 ~ file: polyvPlayer.vue:173 ~ seekVideo ~ time:", time);
  224. time = time || 0;
  225. // #ifdef MP-WEIXIN
  226. polyvPlayerContext.seek(time);
  227. // #endif
  228. // #ifdef H5
  229. polyvPlayerContext.j2s_seekVideo(time);
  230. // #endif
  231. },
  232. restart() {
  233. this.seekVideo(0);
  234. this.closeToast();
  235. },
  236. // 播放时刻
  237. playCurrentTime() {
  238. if (!polyvPlayerContext) {
  239. return 0;
  240. }
  241. // #ifdef MP-WEIXIN
  242. return polyvPlayerContext.getCurrentTime(); //播放时刻
  243. // #endif
  244. // #ifdef H5
  245. return polyvPlayerContext.j2s_getCurrentTime();
  246. // #endif
  247. },
  248. getDuration() {
  249. // #ifdef MP-WEIXIN
  250. return polyvPlayerContext.getDuration();
  251. // #endif
  252. // #ifdef H5
  253. return polyvPlayerContext.j2s_getDuration();
  254. // #endif
  255. },
  256. // 本次看的时长
  257. playVideoTime() {
  258. if (!polyvPlayerContext) {
  259. return 0;
  260. }
  261. // #ifdef MP-WEIXIN
  262. return polyvPlayerContext.getVideoPlayDuration();
  263. // #endif
  264. // #ifdef H5
  265. return polyvPlayerContext.j2s_realPlayVideoTime();
  266. // #endif
  267. },
  268. // 暂停播放
  269. playPause() {
  270. // #ifdef MP-WEIXIN
  271. polyvPlayerContext.pause();
  272. // #endif
  273. // #ifdef H5
  274. polyvPlayerContext.j2s_pauseVideo();
  275. // #endif
  276. },
  277. // 退出全屏
  278. exitFullScreen() {
  279. if (!polyvPlayerContext) {
  280. return;
  281. }
  282. // #ifdef MP-WEIXIN
  283. polyvPlayerContext.exitFullScreen();
  284. // #endif
  285. // #ifdef H5
  286. if (
  287. !!(
  288. document.webkitIsFullScreen ||
  289. document.mozFullScreen ||
  290. document.msFullscreenElement ||
  291. document.fullscreenElement
  292. )
  293. ) {
  294. try {
  295. var de = document;
  296. if (de.exitFullscreen) {
  297. de.exitFullscreen();
  298. } else if (de.mozCancelFullScreen) {
  299. de.mozCancelFullScreen();
  300. } else if (de.webkitCancelFullScreen) {
  301. de.webkitCancelFullScreen();
  302. }
  303. } catch (err) {}
  304. }
  305. // #endif
  306. },
  307. onPlayerInitOver() {
  308. uni.$on("playPause", this.playPause);
  309. this.$emit("loadedmetadata", polyvPlayerContext);
  310. },
  311. onPlayStart() {},
  312. loadedmetadata() {
  313. if (this.hasStart) {
  314. // 防止loadedmetadata事件第二次触发
  315. return;
  316. }
  317. this.hasStart = true;
  318. setTimeout(() => {
  319. this.hasStart = false;
  320. }, 3000);
  321. // #ifdef MP-WEIXIN
  322. polyvPlayerContext = this.selectComponent("#playerVideo");
  323. // #endif
  324. this.onPlayerInitOver();
  325. },
  326. changeVid(data) {
  327. if (!data) {
  328. return;
  329. }
  330. this.config = this.$method.isObject(data)
  331. ? data
  332. : { vid: data, videoCurrentTime: this.videoCurrentTime };
  333. if (!this.vid) {
  334. return;
  335. }
  336. // #ifdef H5
  337. this.loadPlayerScript(this.loadPlayer);
  338. // #endif
  339. // #ifdef MP-WEIXIN
  340. if (polyvPlayerContext) {
  341. polyvPlayerContext.changeVid(this.vid);
  342. this.seekVideo(this.videoCurrentTime);
  343. }
  344. // #endif
  345. },
  346. closeToast() {
  347. this.videoToastShow = false;
  348. },
  349. },
  350. destroyed() {
  351. if (polyvPlayerContext) {
  352. polyvPlayerContext.destroy();
  353. }
  354. // #ifdef H5
  355. this.Elevideo &&
  356. this.Elevideo.removeEventListener("timeupdate", this.timeupdate, false);
  357. // #endif
  358. polyvPlayerContext = null;
  359. },
  360. computed: {
  361. vid() {
  362. return this.config ? this.config.vid : this.playVid;
  363. },
  364. vct() {
  365. return (
  366. (this.config ? this.config.videoCurrentTime : this.videoCurrentTime) ||
  367. 0
  368. );
  369. },
  370. },
  371. watch: {
  372. vct: {
  373. handler(time) {
  374. if (time > 0 && !this.videoToastShow) {
  375. this.videoToastShow = true;
  376. setTimeout(() => {
  377. this.closeToast();
  378. }, 3000);
  379. }
  380. },
  381. immediate: true,
  382. },
  383. },
  384. };
  385. </script>
  386. <style lang="scss" scoped>
  387. .player_box {
  388. position: relative;
  389. #playerVideo {
  390. position: relative;
  391. z-index: 99;
  392. width: 200rpx;
  393. height: 200rpx;
  394. }
  395. .video-toast {
  396. position: absolute;
  397. width: 686rpx;
  398. height: 80rpx;
  399. background: rgba(0, 0, 0, 0.6);
  400. border-radius: 24rpx;
  401. bottom: 100rpx;
  402. left: 50%;
  403. transform: translateX(-50%);
  404. color: #fff;
  405. display: flex;
  406. font-size: 26rpx;
  407. align-items: center;
  408. overflow: visible;
  409. z-index: 999;
  410. &__text {
  411. flex: 1;
  412. margin-left: 40rpx;
  413. }
  414. &__btn {
  415. width: 180rpx;
  416. text-align: center;
  417. border-left: 1rpx solid #fff;
  418. }
  419. }
  420. .video-toast__close {
  421. position: absolute;
  422. right: 32rpx;
  423. bottom: 184rpx;
  424. width: 40rpx;
  425. height: 40rpx;
  426. line-height: 40rpx;
  427. text-align: center;
  428. background: rgba(0, 0, 0, 0.6);
  429. border-radius: 50%;
  430. color: rgba(255, 255, 255, 0.3);
  431. }
  432. }
  433. </style>