polyvPlayer.vue 9.9 KB

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