polyvPlayer.vue 12 KB

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