polyvPlayer.vue 12 KB

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