polyvPlayer.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. isInitSeek: false,
  92. needSeek: false,
  93. playTime: 0,
  94. };
  95. },
  96. created() {
  97. polyvPlayerContext = null;
  98. },
  99. mounted() {
  100. if (this.vid) {
  101. this.changeVid(this.vid);
  102. }
  103. },
  104. methods: {
  105. loadPlayerScript(callback) {
  106. if (!window.polyvPlayer) {
  107. const myScript = document.createElement("script");
  108. myScript.setAttribute("src", this.vodPlayerJs);
  109. myScript.onload = callback;
  110. document.body.appendChild(myScript);
  111. } else {
  112. callback();
  113. }
  114. },
  115. // 播放视频
  116. loadPlayer() {
  117. const polyvPlayer = window.polyvPlayer;
  118. this.$api.polyvVideoSign(this.vid).then(async (res) => {
  119. let option = {
  120. showLine: "off",
  121. ban_history_time: "on",
  122. vid: this.vid,
  123. forceH5: true,
  124. autoplay: this.autoplay, // 自动播放
  125. ban_seek: this.allowSeek, // 是否禁止拖拽进度条
  126. speed: this.playbackRate, // 倍数
  127. banSeekDeviation: 7, // 做兼容
  128. teaser_show: 1,
  129. tail_show: 1,
  130. hideSwitchPlayer: true,
  131. watchStartTime: this.videoCurrentTime, // 播放开始时间,表示视频从第几秒开始播放,参数值需小于视频时长
  132. ts: res.data.data.ts, // 移动播放加密视频需传入的时间戳。
  133. sign: res.data.data.sign, // 移动端播放加密视频所需的签名
  134. };
  135. if (polyvPlayerContext) {
  136. polyvPlayerContext.changeVid(option);
  137. } else {
  138. option = {
  139. wrap: "#player",
  140. width: "100%",
  141. height: 218,
  142. ...option,
  143. };
  144. polyvPlayerContext = polyvPlayer(option);
  145. this.h5StateChange();
  146. }
  147. });
  148. },
  149. wxStatechange(newstate) {
  150. console.log(
  151. "🚀 ~ file: polyvPlayer.vue:152 ~ wxStatechange ~ newstate:",
  152. newstate
  153. );
  154. polyvPlayerContext = this.selectComponent("#playerVideo");
  155. // ["playing", "pause", "ended","error"]
  156. let state = newstate.detail.newstate;
  157. if (state == "error") {
  158. state = "playerError";
  159. }
  160. this[state] && this[state]();
  161. this.$emit(state);
  162. },
  163. playing() {
  164. // 设置播放时间会重复触发
  165. if (this.needSeek) {
  166. this.videoToastShow = true;
  167. setTimeout(() => {
  168. this.closeToast();
  169. }, 3000);
  170. // #ifdef MP-WEIXIN
  171. this.seekVideo(this.videoCurrentTime);
  172. // #endif
  173. this.$emit("studyLog");
  174. this.needSeek = false;
  175. }
  176. // #ifdef H5
  177. if (!this.Elevideo) {
  178. this.Elevideo = document.querySelector("video.plv-player-video");
  179. this.Elevideo &&
  180. this.Elevideo.addEventListener("timeupdate", this.timeupdate);
  181. }
  182. // #endif
  183. },
  184. resumeVideo() {
  185. // #ifdef MP-WEIXIN
  186. polyvPlayerContext.play();
  187. // #endif
  188. // #ifdef H5
  189. polyvPlayerContext.j2s_resumeVideo();
  190. // #endif
  191. },
  192. playerError(err) {
  193. console.log("播放err", err);
  194. },
  195. timeupdate(e) {
  196. let time = 0;
  197. // #ifdef MP-WEIXIN
  198. time = e.detail.currentTime;
  199. // #endif
  200. // #ifdef H5
  201. time = this.playCurrentTime();
  202. if (!this.isInitSeek) {
  203. if (time >= this.vct) {
  204. this.isInitSeek = true;
  205. } else {
  206. return;
  207. }
  208. }
  209. this.onVideoSeek1(time);
  210. // #endif
  211. this.$emit("timeupdate", time);
  212. },
  213. h5StateChange() {
  214. let states = {
  215. s2j_onPlayerInitOver: "onPlayerInitOver", // 播放器初始化完毕时触发
  216. s2j_onPlayStart: "onPlayStart", // 视频初次播放时触发
  217. s2j_onVideoPause: "pause", // 视频暂停时触发
  218. s2j_onVideoPlay: "playing", // 视频初次播放或由暂停恢复播放时触发
  219. s2j_onPlayOver: "ended", // 当前视频播放完毕时触发
  220. s2j_onVideoSeek: "onVideoSeek", // 视频拖拽进度时触发
  221. s2j_onPlayerError: "playerError", // 播放出现错误时触发
  222. serverError: "playerError", // 逻辑错误
  223. };
  224. let that = this;
  225. for (const key in states) {
  226. polyvPlayerContext.on(key, function () {
  227. that[states[key]] && that[states[key]](...arguments);
  228. that.$emit(states[key]);
  229. });
  230. }
  231. },
  232. onVideoSeek(start, end, vid) {
  233. // if (this.allowSeek == "off") {
  234. // return;
  235. // }
  236. // if (end - this.playTime > 10) {
  237. // this.seekVideo(start);
  238. // }
  239. },
  240. // 新增用户视频学习日志
  241. studyLog() {
  242. this.$http({
  243. url: "/user/study/log",
  244. method: "post",
  245. data: {
  246. goodsId: this.goodsId,
  247. courseId: this.courseId,
  248. moduleId: this.moduleId || 0,
  249. chapterId: this.chapterId || 0,
  250. sectionId: this.playSectionId || 0,
  251. fromPlat: 1, //来源平台 1小程序 2PC网站
  252. goodsType: 1, // 商品类型 1视频2题库 3补考 4前培 5虚拟赠送题库 6直播
  253. orderGoodsId: this.orderGoodsId,
  254. },
  255. }).then((res) => {});
  256. },
  257. onVideoSeek1(time) {
  258. if (this.allowSeek == "off") {
  259. return;
  260. }
  261. if (Math.abs(time - this.playTime) > 5) {
  262. console.log("进度条回拉", this.playTime, this.vct);
  263. this.seekVideo(this.playTime);
  264. } else {
  265. this.playTime = time;
  266. }
  267. },
  268. seekVideo(time) {
  269. time = time || 0;
  270. // #ifdef MP-WEIXIN
  271. polyvPlayerContext.seek(time);
  272. // #endif
  273. // #ifdef H5
  274. polyvPlayerContext.j2s_seekVideo(time);
  275. // #endif
  276. },
  277. restart() {
  278. this.seekVideo(0);
  279. this.closeToast();
  280. },
  281. // 播放时刻
  282. playCurrentTime() {
  283. if (!polyvPlayerContext) {
  284. return 0;
  285. }
  286. // #ifdef MP-WEIXIN
  287. return polyvPlayerContext.getCurrentTime(); //播放时刻
  288. // #endif
  289. // #ifdef H5
  290. return polyvPlayerContext.j2s_getCurrentTime();
  291. // #endif
  292. },
  293. getDuration() {
  294. // #ifdef MP-WEIXIN
  295. return polyvPlayerContext.getDuration();
  296. // #endif
  297. // #ifdef H5
  298. return polyvPlayerContext.j2s_getDuration();
  299. // #endif
  300. },
  301. // 本次看的时长
  302. playVideoTime() {
  303. if (!polyvPlayerContext) {
  304. return 0;
  305. }
  306. // #ifdef MP-WEIXIN
  307. return polyvPlayerContext.getVideoPlayDuration();
  308. // #endif
  309. // #ifdef H5
  310. return polyvPlayerContext.j2s_realPlayVideoTime();
  311. // #endif
  312. },
  313. // 暂停播放
  314. playPause() {
  315. if (!polyvPlayerContext) {
  316. return;
  317. }
  318. // #ifdef MP-WEIXIN
  319. polyvPlayerContext.pause();
  320. // #endif
  321. // #ifdef H5
  322. polyvPlayerContext.j2s_pauseVideo();
  323. // #endif
  324. },
  325. // 退出全屏
  326. exitFullScreen() {
  327. if (!polyvPlayerContext) {
  328. return;
  329. }
  330. // #ifdef MP-WEIXIN
  331. polyvPlayerContext.exitFullScreen();
  332. // #endif
  333. // #ifdef H5
  334. if (
  335. !!(
  336. document.webkitIsFullScreen ||
  337. document.mozFullScreen ||
  338. document.msFullscreenElement ||
  339. document.fullscreenElement
  340. )
  341. ) {
  342. try {
  343. var de = document;
  344. if (de.exitFullscreen) {
  345. de.exitFullscreen();
  346. } else if (de.mozCancelFullScreen) {
  347. de.mozCancelFullScreen();
  348. } else if (de.webkitCancelFullScreen) {
  349. de.webkitCancelFullScreen();
  350. }
  351. } catch (err) {}
  352. }
  353. // #endif
  354. },
  355. onPlayerInitOver() {
  356. uni.$on("playPause", this.playPause);
  357. this.$emit("loadedmetadata", polyvPlayerContext);
  358. },
  359. onPlayStart() {},
  360. loadedmetadata() {
  361. if (this.hasStart) {
  362. // 防止loadedmetadata事件第二次触发
  363. return;
  364. }
  365. this.hasStart = true;
  366. setTimeout(() => {
  367. this.hasStart = false;
  368. }, 3000);
  369. // #ifdef MP-WEIXIN
  370. polyvPlayerContext = this.selectComponent("#playerVideo");
  371. // #endif
  372. this.onPlayerInitOver();
  373. },
  374. changeVid(data) {
  375. if (!data) {
  376. return;
  377. }
  378. this.config = this.$method.isObject(data)
  379. ? data
  380. : { vid: data, videoCurrentTime: this.videoCurrentTime };
  381. if (!this.vid) {
  382. return;
  383. }
  384. // #ifdef H5
  385. this.loadPlayerScript(this.loadPlayer);
  386. // #endif
  387. // #ifdef MP-WEIXIN
  388. if (polyvPlayerContext) {
  389. polyvPlayerContext.changeVid(this.vid);
  390. this.seekVideo(this.videoCurrentTime);
  391. }
  392. // #endif
  393. },
  394. closeToast() {
  395. this.videoToastShow = false;
  396. },
  397. },
  398. destroyed() {
  399. if (polyvPlayerContext) {
  400. polyvPlayerContext.destroy();
  401. }
  402. // #ifdef H5
  403. this.Elevideo &&
  404. this.Elevideo.removeEventListener("timeupdate", this.timeupdate, false);
  405. // #endif
  406. polyvPlayerContext = null;
  407. },
  408. computed: {
  409. vid() {
  410. return this.config ? this.config.vid : this.playVid;
  411. },
  412. vct() {
  413. return (
  414. (this.config ? this.config.videoCurrentTime : this.videoCurrentTime) ||
  415. 0
  416. );
  417. },
  418. },
  419. watch: {
  420. vct: {
  421. handler(time) {
  422. if (time > 0) {
  423. this.needSeek = true;
  424. this.playTime = time;
  425. }
  426. },
  427. immediate: true,
  428. },
  429. },
  430. };
  431. </script>
  432. <style lang="scss" scoped>
  433. .player_box {
  434. position: relative;
  435. #playerVideo {
  436. position: relative;
  437. z-index: 99;
  438. width: 200rpx;
  439. height: 200rpx;
  440. }
  441. .video-toast {
  442. position: absolute;
  443. width: 686rpx;
  444. height: 80rpx;
  445. background: rgba(0, 0, 0, 0.6);
  446. border-radius: 24rpx;
  447. bottom: 100rpx;
  448. left: 50%;
  449. transform: translateX(-50%);
  450. color: #fff;
  451. display: flex;
  452. font-size: 26rpx;
  453. align-items: center;
  454. overflow: visible;
  455. z-index: 999;
  456. &__text {
  457. flex: 1;
  458. margin-left: 40rpx;
  459. }
  460. &__btn {
  461. width: 180rpx;
  462. text-align: center;
  463. border-left: 1rpx solid #fff;
  464. }
  465. }
  466. .video-toast__close {
  467. position: absolute;
  468. right: 32rpx;
  469. bottom: 184rpx;
  470. width: 40rpx;
  471. height: 40rpx;
  472. line-height: 40rpx;
  473. text-align: center;
  474. background: rgba(0, 0, 0, 0.6);
  475. border-radius: 50%;
  476. color: rgba(255, 255, 255, 0.3);
  477. }
  478. }
  479. </style>