polyvPlayer.vue 12 KB

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