polyvPlayer.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. :autoplay="autoplay"
  17. :page-gesture="true"
  18. :vslide-gesture="true"
  19. :vslide-gesture-in-fullscreen="true"
  20. :isAllowSeek="allowSeek"
  21. :playbackRate="playbackRate"
  22. :enableAutoRotation="enableAutoRotation"
  23. @loadedmetadata="loadedmetadata"
  24. ></polyv-player>
  25. <!-- #endif -->
  26. <!-- #ifdef H5 -->
  27. <view id="player"></view>
  28. <!-- #endif -->
  29. <template v-if="videoToastShow">
  30. <cover-view class="video-toast__close" @click="closeToast()"
  31. >X</cover-view
  32. >
  33. <cover-view class="video-toast">
  34. <cover-view class="video-toast__text"
  35. >您上次看到
  36. {{ $method.secondToDate(vct) }},正在自动续播</cover-view
  37. >
  38. <cover-view class="video-toast__btn" @click="restart()"
  39. >从头播放</cover-view
  40. >
  41. </cover-view>
  42. </template>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. var polyvPlayerContext = null;
  48. export default {
  49. name: "SaasMiniprogramPolyvPlayer",
  50. props: {
  51. playVid: {
  52. type: String,
  53. defaule: "",
  54. },
  55. autoplay: {
  56. type: Boolean,
  57. defaule: false,
  58. },
  59. allowSeek: {
  60. type: String,
  61. defaule: () => {
  62. // #ifdef MP-WEIXIN
  63. return "no";
  64. // #endif
  65. // #ifdef H5
  66. return "on";
  67. // #endif
  68. },
  69. },
  70. videoCurrentTime: {
  71. type: Number,
  72. defaule: 0,
  73. },
  74. playbackRate: {
  75. type: Array,
  76. defaule: () => {
  77. return [1.0];
  78. },
  79. },
  80. },
  81. data() {
  82. return {
  83. barTimer: null,
  84. vodPlayerJs: "https://player.polyv.net/resp/vod-player/latest/player.js",
  85. videoToastShow: false,
  86. enableAutoRotation: true,
  87. hasStart: false,
  88. config: null,
  89. Elevideo: null,
  90. };
  91. },
  92. created() {
  93. polyvPlayerContext = null;
  94. },
  95. mounted() {
  96. if (this.vid) {
  97. this.changeVid(this.vid);
  98. }
  99. // if (this.vct > 0) {
  100. // this.videoToastShow = true;
  101. // setTimeout(() => {
  102. // this.closeToast();
  103. // }, 3000);
  104. // }
  105. },
  106. methods: {
  107. loadPlayerScript(callback) {
  108. if (!window.polyvPlayer) {
  109. const myScript = document.createElement("script");
  110. myScript.setAttribute("src", this.vodPlayerJs);
  111. myScript.onload = callback;
  112. document.body.appendChild(myScript);
  113. } else {
  114. callback();
  115. }
  116. },
  117. // 播放视频
  118. loadPlayer() {
  119. const polyvPlayer = window.polyvPlayer;
  120. this.$api.polyvVideoSign(this.vid).then(async (res) => {
  121. let option = {
  122. showLine: "off",
  123. ban_history_time: "on",
  124. vid: this.vid,
  125. forceH5: true,
  126. autoplay: this.autoplay, // 自动播放
  127. ban_seek: this.allowSeek, // 是否禁止拖拽进度条
  128. speed: this.playbackRate, // 倍数
  129. banSeekDeviation: 7, // 做兼容
  130. teaser_show: 1,
  131. tail_show: 1,
  132. hideSwitchPlayer: true,
  133. watchStartTime: this.videoCurrentTime, // 播放开始时间,表示视频从第几秒开始播放,参数值需小于视频时长
  134. ts: res.data.data.ts, // 移动播放加密视频需传入的时间戳。
  135. sign: res.data.data.sign, // 移动端播放加密视频所需的签名
  136. };
  137. if (polyvPlayerContext) {
  138. polyvPlayerContext.changeVid(option);
  139. } else {
  140. option = {
  141. wrap: "#player",
  142. width: "100%",
  143. height: 218,
  144. ...option,
  145. };
  146. polyvPlayerContext = polyvPlayer(option);
  147. this.h5StateChange();
  148. }
  149. });
  150. },
  151. wxStatechange(newstate) {
  152. console.log("lwxStatechange", newstate);
  153. // ["playing", "pause", "ended","error"]
  154. let state = newstate.detail.newstate;
  155. if (state == "error") {
  156. state = "playerError";
  157. }
  158. this[state] && this[state]();
  159. this.$emit(state);
  160. },
  161. playing() {
  162. // #ifdef MP-WEIXIN
  163. polyvPlayerContext.seek(this.videoCurrentTime || 0);
  164. // #endif
  165. // #ifdef H5
  166. this.Elevideo = document.querySelector("video.plv-player-video");
  167. this.Elevideo &&
  168. this.Elevideo.addEventListener("timeupdate", this.timeupdate);
  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. this.$emit("timeupdate", this.playCurrentTime());
  184. },
  185. h5StateChange() {
  186. let states = {
  187. s2j_onPlayerInitOver: "onPlayerInitOver", // 播放器初始化完毕时触发
  188. s2j_onPlayStart: "onPlayStart", // 视频初次播放时触发
  189. s2j_onVideoPause: "pause", // 视频暂停时触发
  190. s2j_onVideoPlay: "playing", // 视频初次播放或由暂停恢复播放时触发
  191. s2j_onPlayOver: "ended", // 当前视频播放完毕时触发
  192. s2j_onVideoSeek: "onVideoSeek", // 视频拖拽进度时触发
  193. s2j_onPlayerError: "playerError", // 播放出现错误时触发
  194. serverError: "playerError", // 逻辑错误
  195. };
  196. let that = this;
  197. for (const key in states) {
  198. polyvPlayerContext.on(key, function () {
  199. that[states[key]] && that[states[key]](...arguments);
  200. that.$emit(states[key]);
  201. });
  202. }
  203. },
  204. onVideoSeek(start, end, vid) {
  205. polyvPlayerContext.toggleFullscreen();
  206. if (this.allowSeek !== "on") {
  207. return;
  208. }
  209. if (end - start > 10) {
  210. polyvPlayerContext.j2s_seekVideo(start);
  211. }
  212. },
  213. seekVideo(time) {
  214. console.log("🚀 ~ file: polyvPlayer.vue:173 ~ seekVideo ~ time:", time);
  215. time = time || 0;
  216. // #ifdef MP-WEIXIN
  217. polyvPlayerContext.seek(time);
  218. // #endif
  219. // #ifdef H5
  220. polyvPlayerContext.j2s_seekVideo(time);
  221. // #endif
  222. },
  223. restart() {
  224. this.seekVideo(0);
  225. this.closeToast();
  226. },
  227. // 播放时刻
  228. playCurrentTime() {
  229. if (!polyvPlayerContext) {
  230. return 0;
  231. }
  232. // #ifdef MP-WEIXIN
  233. return polyvPlayerContext.getCurrentTime(); //播放时刻
  234. // #endif
  235. // #ifdef H5
  236. return polyvPlayerContext.j2s_getCurrentTime();
  237. // #endif
  238. },
  239. getDuration() {
  240. // #ifdef MP-WEIXIN
  241. return polyvPlayerContext.getDuration();
  242. // #endif
  243. // #ifdef H5
  244. return polyvPlayerContext.j2s_getDuration();
  245. // #endif
  246. },
  247. // 本次看的时长
  248. playVideoTime() {
  249. if (!polyvPlayerContext) {
  250. return 0;
  251. }
  252. // #ifdef MP-WEIXIN
  253. return polyvPlayerContext.getVideoPlayDuration();
  254. // #endif
  255. // #ifdef H5
  256. return polyvPlayerContext.j2s_realPlayVideoTime();
  257. // #endif
  258. },
  259. // 暂停播放
  260. playPause() {
  261. // #ifdef MP-WEIXIN
  262. polyvPlayerContext.pause();
  263. // #endif
  264. // #ifdef H5
  265. polyvPlayerContext.j2s_pauseVideo();
  266. // #endif
  267. },
  268. // 退出全屏
  269. exitFullScreen() {
  270. // #ifdef MP-WEIXIN
  271. polyvPlayerContext.exitFullScreen();
  272. // #endif
  273. // #ifdef H5
  274. polyvPlayerContext.toggleFullscreen();
  275. // #endif
  276. },
  277. onPlayerInitOver() {
  278. uni.$on("playPause", this.playPause);
  279. this.$emit("loadedmetadata", polyvPlayerContext);
  280. },
  281. onPlayStart() {},
  282. loadedmetadata() {
  283. if (this.hasStart) {
  284. // 防止loadedmetadata事件第二次触发
  285. return;
  286. }
  287. this.hasStart = true;
  288. setTimeout(() => {
  289. this.hasStart = false;
  290. }, 3000);
  291. // #ifdef MP-WEIXIN
  292. polyvPlayerContext = this.selectComponent("#playerVideo");
  293. // #endif
  294. this.onPlayerInitOver();
  295. },
  296. changeVid(data) {
  297. if (!data) {
  298. return;
  299. }
  300. this.config = this.$method.isObject(data)
  301. ? data
  302. : { vid: data, videoCurrentTime: this.videoCurrentTime };
  303. if (!this.vid) {
  304. return;
  305. }
  306. // #ifdef H5
  307. this.loadPlayerScript(this.loadPlayer);
  308. // #endif
  309. // #ifdef MP-WEIXIN
  310. if (polyvPlayerContext) {
  311. polyvPlayerContext.changeVid(this.vid);
  312. this.seekVideo(this.videoCurrentTime);
  313. }
  314. // #endif
  315. },
  316. closeToast() {
  317. this.videoToastShow = false;
  318. },
  319. },
  320. destroyed() {
  321. if (polyvPlayerContext) {
  322. polyvPlayerContext.destroy();
  323. }
  324. // #ifdef H5
  325. this.Elevideo &&
  326. this.Elevideo.removeEventListener("timeupdate", this.timeupdate, false);
  327. // #endif
  328. },
  329. computed: {
  330. vid() {
  331. return this.config ? this.config.vid : this.playVid;
  332. },
  333. vct() {
  334. return (
  335. (this.config ? this.config.videoCurrentTime : this.videoCurrentTime) ||
  336. 0
  337. );
  338. },
  339. },
  340. watch: {
  341. vct: {
  342. handler(time) {
  343. if (time > 0 && !this.videoToastShow) {
  344. this.videoToastShow = true;
  345. setTimeout(() => {
  346. this.closeToast();
  347. }, 3000);
  348. }
  349. },
  350. immediate: true,
  351. },
  352. },
  353. };
  354. </script>
  355. <style lang="scss" scoped>
  356. .player_box {
  357. position: relative;
  358. #playerVideo {
  359. position: relative;
  360. z-index: 99;
  361. width: 200rpx;
  362. height: 200rpx;
  363. }
  364. .video-toast {
  365. position: absolute;
  366. width: 686rpx;
  367. height: 80rpx;
  368. background: rgba(0, 0, 0, 0.6);
  369. border-radius: 24rpx;
  370. bottom: 100rpx;
  371. left: 50%;
  372. transform: translateX(-50%);
  373. color: #fff;
  374. display: flex;
  375. font-size: 26rpx;
  376. align-items: center;
  377. overflow: visible;
  378. z-index: 999;
  379. &__text {
  380. flex: 1;
  381. margin-left: 40rpx;
  382. }
  383. &__btn {
  384. width: 180rpx;
  385. text-align: center;
  386. border-left: 1rpx solid #fff;
  387. }
  388. }
  389. .video-toast__close {
  390. position: absolute;
  391. right: 32rpx;
  392. bottom: 184rpx;
  393. width: 40rpx;
  394. height: 40rpx;
  395. line-height: 40rpx;
  396. text-align: center;
  397. background: rgba(0, 0, 0, 0.6);
  398. border-radius: 50%;
  399. color: rgba(255, 255, 255, 0.3);
  400. }
  401. }
  402. </style>