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 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. speed: playbackRate, // 倍数
  113. banSeekDeviation: 7, // 做兼容
  114. teaser_show: 0,
  115. tail_show: 1,
  116. hideSwitchPlayer: true,
  117. watchStartTime: this.videoCurrentTime, // 播放开始时间,表示视频从第几秒开始播放,参数值需小于视频时长
  118. ts: res.data.data.ts, // 移动播放加密视频需传入的时间戳。
  119. sign: res.data.data.sign, // 移动端播放加密视频所需的签名
  120. };
  121. if (polyvPlayerContext) {
  122. polyvPlayerContext.changeVid(option);
  123. } else {
  124. option = {
  125. wrap: "#player",
  126. width: "100%",
  127. height: 218,
  128. ...option,
  129. };
  130. polyvPlayerContext = polyvPlayer(option);
  131. this.h5StateChange();
  132. }
  133. });
  134. },
  135. wxStatechange(newstate) {
  136. polyvPlayerContext = this.selectComponent("#playerVideo");
  137. // ["playing", "pause", "ended","error"]
  138. let state = newstate.detail.newstate;
  139. if (state == "error") {
  140. state = "playerError";
  141. }
  142. this[state] && this[state]();
  143. this.$emit(state);
  144. },
  145. playing() {
  146. // 设置播放时间会重复触发
  147. if (this.needSeek) {
  148. // #ifdef H5
  149. this.videoToastShow = true;
  150. // #endif
  151. // #ifdef MP-WEIXIN
  152. this.videoToastShow = true;
  153. // #endif
  154. setTimeout(() => {
  155. this.closeToast();
  156. }, 3000);
  157. // #ifdef MP-WEIXIN
  158. this.seekVideo(this.videoCurrentTime);
  159. // #endif
  160. this.$emit("studyLog");
  161. this.needSeek = false;
  162. }
  163. // #ifdef H5
  164. if (!this.Elevideo) {
  165. this.Elevideo = document.querySelector("video.plv-player-video");
  166. this.Elevideo &&
  167. this.Elevideo.addEventListener("timeupdate", this.timeupdate);
  168. }
  169. // #endif
  170. },
  171. resumeVideo() {
  172. // #ifdef MP-WEIXIN
  173. polyvPlayerContext.play();
  174. // #endif
  175. // #ifdef H5
  176. polyvPlayerContext.j2s_resumeVideo();
  177. // #endif
  178. },
  179. playerError(err) {
  180. console.log("播放err", err);
  181. },
  182. timeupdate(e) {
  183. let time = 0;
  184. // #ifdef MP-WEIXIN
  185. time = e.detail.currentTime;
  186. // #endif
  187. // #ifdef H5
  188. if (!this.PlayStart) return
  189. time = this.PlayStart == 1 ? (this.videoCurrentTime || 0) : this.playCurrentTime();
  190. // if (!this.isInitSeek) {
  191. // if (time >= this.vct) {
  192. // this.isInitSeek = true;
  193. // } else {
  194. // return;
  195. // }
  196. // }
  197. // this.onVideoSeek1(time);
  198. this.PlayStart++
  199. // #endif
  200. this.$emit("timeupdate", time);
  201. },
  202. h5StateChange() {
  203. let states = {
  204. s2j_onPlayerInitOver: "onPlayerInitOver", // 播放器初始化完毕时触发
  205. s2j_onPlayStart: "onPlayStart", // 视频初次播放时触发
  206. s2j_onVideoPause: "pause", // 视频暂停时触发
  207. s2j_onVideoPlay: "playing", // 视频初次播放或由暂停恢复播放时触发
  208. s2j_onPlayOver: "ended", // 当前视频播放完毕时触发
  209. s2j_onVideoSeek: "onVideoSeek", // 视频拖拽进度时触发
  210. s2j_onPlayerError: "playerError", // 播放出现错误时触发
  211. serverError: "playerError", // 逻辑错误
  212. };
  213. let that = this;
  214. for (const key in states) {
  215. polyvPlayerContext.on(key, function() {
  216. that[states[key]] && that[states[key]](...arguments);
  217. that.$emit(states[key]);
  218. });
  219. }
  220. },
  221. onVideoSeek(start, end, vid) {
  222. // if (this.allowSeek == "off") {
  223. // return;
  224. // }
  225. // if (end - this.playTime > 10) {
  226. // this.seekVideo(start);
  227. // }
  228. },
  229. // 新增用户视频学习日志
  230. studyLog() {
  231. this.$http({
  232. url: "/user/study/log",
  233. method: "post",
  234. data: {
  235. goodsId: this.goodsId,
  236. courseId: this.courseId,
  237. moduleId: this.moduleId || 0,
  238. chapterId: this.chapterId || 0,
  239. sectionId: this.playSectionId || 0,
  240. fromPlat: 1, //来源平台 1小程序 2PC网站
  241. goodsType: 1, // 商品类型 1视频2题库 3补考 4前培 5虚拟赠送题库 6直播
  242. orderGoodsId: this.orderGoodsId,
  243. },
  244. }).then((res) => {});
  245. },
  246. onVideoSeek1(time) {
  247. if (this.allowSeek == "off") {
  248. return;
  249. }
  250. if (Math.abs(time - this.playTime) > 5) {
  251. console.log("进度条回拉", this.playTime, this.vct);
  252. this.seekVideo(this.playTime);
  253. } else {
  254. this.playTime = time;
  255. }
  256. },
  257. seekVideo(time) {
  258. time = time || 0;
  259. // #ifdef MP-WEIXIN
  260. polyvPlayerContext.seek(time);
  261. // #endif
  262. // #ifdef H5
  263. polyvPlayerContext.j2s_seekVideo(time);
  264. // #endif
  265. },
  266. restart() {
  267. this.seekVideo(0);
  268. this.closeToast();
  269. },
  270. // 播放时刻
  271. playCurrentTime() {
  272. if (!polyvPlayerContext) {
  273. return 0;
  274. }
  275. // #ifdef MP-WEIXIN
  276. return polyvPlayerContext.getCurrentTime(); //播放时刻
  277. // #endif
  278. // #ifdef H5
  279. return polyvPlayerContext.j2s_getCurrentTime();
  280. // #endif
  281. },
  282. getDuration() {
  283. // #ifdef MP-WEIXIN
  284. return polyvPlayerContext.getDuration();
  285. // #endif
  286. // #ifdef H5
  287. return polyvPlayerContext.j2s_getDuration();
  288. // #endif
  289. },
  290. // 本次看的时长
  291. playVideoTime() {
  292. if (!polyvPlayerContext) {
  293. return 0;
  294. }
  295. // #ifdef MP-WEIXIN
  296. return polyvPlayerContext.getVideoPlayDuration();
  297. // #endif
  298. // #ifdef H5
  299. return polyvPlayerContext.j2s_realPlayVideoTime();
  300. // #endif
  301. },
  302. // 暂停播放
  303. playPause() {
  304. if (!polyvPlayerContext) {
  305. return;
  306. }
  307. // #ifdef MP-WEIXIN
  308. polyvPlayerContext.pause();
  309. // #endif
  310. // #ifdef H5
  311. polyvPlayerContext.j2s_pauseVideo();
  312. // #endif
  313. },
  314. // 退出全屏
  315. exitFullScreen() {
  316. if (!polyvPlayerContext) {
  317. return;
  318. }
  319. // #ifdef MP-WEIXIN
  320. polyvPlayerContext.exitFullScreen();
  321. // #endif
  322. // #ifdef H5
  323. if (
  324. !!(
  325. document.webkitIsFullScreen ||
  326. document.mozFullScreen ||
  327. document.msFullscreenElement ||
  328. document.fullscreenElement
  329. )
  330. ) {
  331. try {
  332. var de = document;
  333. if (de.exitFullscreen) {
  334. de.exitFullscreen();
  335. } else if (de.mozCancelFullScreen) {
  336. de.mozCancelFullScreen();
  337. } else if (de.webkitCancelFullScreen) {
  338. de.webkitCancelFullScreen();
  339. }
  340. } catch (err) {}
  341. }
  342. // #endif
  343. },
  344. onPlayerInitOver() {
  345. uni.$on("playPause", this.playPause);
  346. this.$emit("loadedmetadata", polyvPlayerContext);
  347. },
  348. onPlayStart() {
  349. console.log("初始化")
  350. this.PlayStart = 1
  351. },
  352. loadedmetadata() {
  353. if (this.hasStart) {
  354. // 防止loadedmetadata事件第二次触发
  355. return;
  356. }
  357. this.hasStart = true;
  358. setTimeout(() => {
  359. this.hasStart = false;
  360. }, 3000);
  361. // #ifdef MP-WEIXIN
  362. polyvPlayerContext = this.selectComponent("#playerVideo");
  363. // #endif
  364. this.onPlayerInitOver();
  365. },
  366. changeVid(data) {
  367. if (!data) {
  368. return;
  369. }
  370. this.config = this.$method.isObject(data) ?
  371. data : {
  372. vid: data,
  373. videoCurrentTime: this.videoCurrentTime
  374. };
  375. if (!this.vid) {
  376. return;
  377. }
  378. this.PlayStart = 0
  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>