polyvPlayer.vue 12 KB

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