polyvPlayer.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. 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. console.log("🚀 ~ file: polyvPlayer.vue:151 ~ wxStatechange ~ newstate:", 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. // #ifdef MP-WEIXIN
  161. this.seekVideo(this.videoCurrentTime);
  162. // #endif
  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. time = this.playCurrentTime();
  189. if (!this.isInitSeek) {
  190. if (time >= this.vct) {
  191. this.isInitSeek = true;
  192. } else {
  193. return;
  194. }
  195. }
  196. this.onVideoSeek1(time);
  197. // #endif
  198. this.$emit("timeupdate", time);
  199. },
  200. h5StateChange() {
  201. let states = {
  202. s2j_onPlayerInitOver: "onPlayerInitOver", // 播放器初始化完毕时触发
  203. s2j_onPlayStart: "onPlayStart", // 视频初次播放时触发
  204. s2j_onVideoPause: "pause", // 视频暂停时触发
  205. s2j_onVideoPlay: "playing", // 视频初次播放或由暂停恢复播放时触发
  206. s2j_onPlayOver: "ended", // 当前视频播放完毕时触发
  207. s2j_onVideoSeek: "onVideoSeek", // 视频拖拽进度时触发
  208. s2j_onPlayerError: "playerError", // 播放出现错误时触发
  209. serverError: "playerError", // 逻辑错误
  210. };
  211. let that = this;
  212. for (const key in states) {
  213. polyvPlayerContext.on(key, function () {
  214. that[states[key]] && that[states[key]](...arguments);
  215. that.$emit(states[key]);
  216. });
  217. }
  218. },
  219. onVideoSeek(start, end, vid) {
  220. // if (this.allowSeek == "off") {
  221. // return;
  222. // }
  223. // if (end - this.playTime > 10) {
  224. // this.seekVideo(start);
  225. // }
  226. },
  227. onVideoSeek1(time) {
  228. if (this.allowSeek == "off") {
  229. return;
  230. }
  231. if (Math.abs(time - this.playTime) > 5) {
  232. this.seekVideo(this.playTime);
  233. } else {
  234. this.playTime = time;
  235. }
  236. },
  237. seekVideo(time) {
  238. time = time || 0;
  239. // #ifdef MP-WEIXIN
  240. polyvPlayerContext.seek(time);
  241. // #endif
  242. // #ifdef H5
  243. polyvPlayerContext.j2s_seekVideo(time);
  244. // #endif
  245. },
  246. restart() {
  247. this.seekVideo(0);
  248. this.closeToast();
  249. },
  250. // 播放时刻
  251. playCurrentTime() {
  252. if (!polyvPlayerContext) {
  253. return 0;
  254. }
  255. // #ifdef MP-WEIXIN
  256. return polyvPlayerContext.getCurrentTime(); //播放时刻
  257. // #endif
  258. // #ifdef H5
  259. return polyvPlayerContext.j2s_getCurrentTime();
  260. // #endif
  261. },
  262. getDuration() {
  263. // #ifdef MP-WEIXIN
  264. return polyvPlayerContext.getDuration();
  265. // #endif
  266. // #ifdef H5
  267. return polyvPlayerContext.j2s_getDuration();
  268. // #endif
  269. },
  270. // 本次看的时长
  271. playVideoTime() {
  272. if (!polyvPlayerContext) {
  273. return 0;
  274. }
  275. // #ifdef MP-WEIXIN
  276. return polyvPlayerContext.getVideoPlayDuration();
  277. // #endif
  278. // #ifdef H5
  279. return polyvPlayerContext.j2s_realPlayVideoTime();
  280. // #endif
  281. },
  282. // 暂停播放
  283. playPause() {
  284. // #ifdef MP-WEIXIN
  285. polyvPlayerContext.pause();
  286. // #endif
  287. // #ifdef H5
  288. polyvPlayerContext.j2s_pauseVideo();
  289. // #endif
  290. },
  291. // 退出全屏
  292. exitFullScreen() {
  293. if (!polyvPlayerContext) {
  294. return;
  295. }
  296. // #ifdef MP-WEIXIN
  297. polyvPlayerContext.exitFullScreen();
  298. // #endif
  299. // #ifdef H5
  300. if (
  301. !!(
  302. document.webkitIsFullScreen ||
  303. document.mozFullScreen ||
  304. document.msFullscreenElement ||
  305. document.fullscreenElement
  306. )
  307. ) {
  308. try {
  309. var de = document;
  310. if (de.exitFullscreen) {
  311. de.exitFullscreen();
  312. } else if (de.mozCancelFullScreen) {
  313. de.mozCancelFullScreen();
  314. } else if (de.webkitCancelFullScreen) {
  315. de.webkitCancelFullScreen();
  316. }
  317. } catch (err) {}
  318. }
  319. // #endif
  320. },
  321. onPlayerInitOver() {
  322. uni.$on("playPause", this.playPause);
  323. this.$emit("loadedmetadata", polyvPlayerContext);
  324. },
  325. onPlayStart() {},
  326. loadedmetadata() {
  327. if (this.hasStart) {
  328. // 防止loadedmetadata事件第二次触发
  329. return;
  330. }
  331. this.hasStart = true;
  332. setTimeout(() => {
  333. this.hasStart = false;
  334. }, 3000);
  335. // #ifdef MP-WEIXIN
  336. polyvPlayerContext = this.selectComponent("#playerVideo");
  337. // #endif
  338. this.onPlayerInitOver();
  339. },
  340. changeVid(data) {
  341. if (!data) {
  342. return;
  343. }
  344. this.config = this.$method.isObject(data)
  345. ? data
  346. : { vid: data, videoCurrentTime: this.videoCurrentTime };
  347. if (!this.vid) {
  348. return;
  349. }
  350. // #ifdef H5
  351. this.loadPlayerScript(this.loadPlayer);
  352. // #endif
  353. // #ifdef MP-WEIXIN
  354. if (polyvPlayerContext) {
  355. polyvPlayerContext.changeVid(this.vid);
  356. this.seekVideo(this.videoCurrentTime);
  357. }
  358. // #endif
  359. },
  360. closeToast() {
  361. this.videoToastShow = false;
  362. },
  363. },
  364. destroyed() {
  365. if (polyvPlayerContext) {
  366. polyvPlayerContext.destroy();
  367. }
  368. // #ifdef H5
  369. this.Elevideo &&
  370. this.Elevideo.removeEventListener("timeupdate", this.timeupdate, false);
  371. // #endif
  372. polyvPlayerContext = null;
  373. },
  374. computed: {
  375. vid() {
  376. return this.config ? this.config.vid : this.playVid;
  377. },
  378. vct() {
  379. return (
  380. (this.config ? this.config.videoCurrentTime : this.videoCurrentTime) ||
  381. 0
  382. );
  383. },
  384. },
  385. watch: {
  386. vct: {
  387. handler(time) {
  388. this.playTime = time;
  389. if (time > 0 && !this.videoToastShow) {
  390. setTimeout(() => {
  391. this.videoToastShow = true;
  392. setTimeout(() => {
  393. this.closeToast();
  394. }, 3000);
  395. }, 8000);
  396. }
  397. },
  398. immediate: true,
  399. },
  400. },
  401. };
  402. </script>
  403. <style lang="scss" scoped>
  404. .player_box {
  405. position: relative;
  406. #playerVideo {
  407. position: relative;
  408. z-index: 99;
  409. width: 200rpx;
  410. height: 200rpx;
  411. }
  412. .video-toast {
  413. position: absolute;
  414. width: 686rpx;
  415. height: 80rpx;
  416. background: rgba(0, 0, 0, 0.6);
  417. border-radius: 24rpx;
  418. bottom: 100rpx;
  419. left: 50%;
  420. transform: translateX(-50%);
  421. color: #fff;
  422. display: flex;
  423. font-size: 26rpx;
  424. align-items: center;
  425. overflow: visible;
  426. z-index: 999;
  427. &__text {
  428. flex: 1;
  429. margin-left: 40rpx;
  430. }
  431. &__btn {
  432. width: 180rpx;
  433. text-align: center;
  434. border-left: 1rpx solid #fff;
  435. }
  436. }
  437. .video-toast__close {
  438. position: absolute;
  439. right: 32rpx;
  440. bottom: 184rpx;
  441. width: 40rpx;
  442. height: 40rpx;
  443. line-height: 40rpx;
  444. text-align: center;
  445. background: rgba(0, 0, 0, 0.6);
  446. border-radius: 50%;
  447. color: rgba(255, 255, 255, 0.3);
  448. }
  449. }
  450. </style>