polyvPlayer.vue 9.5 KB

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