tcPlayer.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <view>
  3. <view class="player_box" style="width: 100%; height: 421rpx">
  4. <view id="player2"></view>
  5. <template v-if="videoToastShow">
  6. <cover-view class="video-toast__close" @click="closeToast()">X</cover-view>
  7. <cover-view class="video-toast">
  8. <cover-view class="video-toast__text">您上次看到
  9. {{ $method.secondToDate(ct) }},正在自动续播</cover-view>
  10. <cover-view class="video-toast__btn" @click="restart()">从头播放</cover-view>
  11. </cover-view>
  12. </template>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. var tcPlayerContext = null;
  18. export default {
  19. name: "SaasMiniprogramTcPlayer",
  20. props: {
  21. playFileId: {
  22. type: String,
  23. default: "",
  24. },
  25. autoplay: {
  26. type: Boolean,
  27. default: false,
  28. },
  29. controlBar: {
  30. type: Boolean,
  31. default: () => {
  32. return true //是否禁止拖拽进度条,取值:{true,false}。
  33. }
  34. },
  35. currentTime: {
  36. type: Number,
  37. default: 0,
  38. },
  39. playbackRate: {
  40. type: Array,
  41. default: () => {
  42. return [1];
  43. },
  44. },
  45. },
  46. data() {
  47. return {
  48. vodPlayerJs: 'https://web.sdk.qcloud.com/player/tcplayer/release/v5.1.0/tcplayer.v5.1.0.min.js',
  49. videoToastShow: false,
  50. hasStart: false,
  51. config: null,
  52. Elevideo: false,
  53. isInitSeek: false,
  54. needSeek: false, // 是否有记录需要跳转播放
  55. playTime: 0,
  56. PlayStart: 0,
  57. isSeeking: false
  58. };
  59. },
  60. created() {
  61. tcPlayerContext = null;
  62. },
  63. mounted() {
  64. if (this.fileId) {
  65. this.changeFileId(this.fileId);
  66. }
  67. },
  68. methods: {
  69. loadPlayerScript(callback) {
  70. if (!window.TCPlayer) {
  71. const myScript = document.createElement("script");
  72. myScript.setAttribute("src", this.vodPlayerJs);
  73. myScript.onload = callback;
  74. document.body.appendChild(myScript);
  75. } else {
  76. callback();
  77. }
  78. },
  79. // 播放视频
  80. loadPlayer() {
  81. if (this.playbackRate.length <= 1) {
  82. var playbackRate = [1];
  83. } else {
  84. var playbackRate = JSON.parse(JSON.stringify(this.playbackRate));
  85. }
  86. const TCPlayer = window.TCPlayer;
  87. console.log("是否显示进度条", this.controlBar);
  88. this.$api.tcVideoSign(this.fileId).then(async (res) => {
  89. let option = {
  90. plugins: {
  91. ContinuePlay: {
  92. auto: true, // 是否禁用续播功能,取值:{true,false}。
  93. SafeCheck: true // 安全检查
  94. },
  95. },
  96. sources: [{
  97. src: 'https://1500005692.vod2.myqcloud.com/43843706vodtranscq1500005692/62656d94387702300542496289/v.f100240.m3u8',
  98. }],
  99. licenseUrl: 'https://license.vod2.myqcloud.com/license/v2/1323759362_1/v_cube.license',
  100. playbackRates: playbackRate,
  101. autoplay: this.autoplay, // 自动播放
  102. controlBar: {
  103. progressControl: this.controlBar
  104. } // 是否禁止拖拽(隐藏)进度条
  105. };
  106. if (tcPlayerContext) {
  107. tcPlayerContext.currentTime(
  108. this.currentTime || 0);
  109. tcPlayerContext.changeFileId(option);
  110. } else {
  111. option = {
  112. height: 218,
  113. ...option,
  114. };
  115. let player_tencent = document.createElement("video");
  116. player_tencent.style.width = "100%";
  117. player_tencent.id = "player-tencent";
  118. player_tencent.setAttribute("playsinline", true);
  119. player_tencent.setAttribute("webkit-playsinline", true);
  120. document
  121. .getElementById("player2")
  122. .insertAdjacentElement("afterend", player_tencent);
  123. tcPlayerContext = TCPlayer("player-tencent", option);
  124. tcPlayerContext.currentTime(
  125. this.currentTime || 0);
  126. this.h5StateChange();
  127. }
  128. });
  129. },
  130. playing() {
  131. if(this.isSeeking) return;
  132. console.log("腾讯视频恢复播放");
  133. if (!this.PlayStart) {
  134. this.onPlayStart();
  135. return;
  136. }
  137. // 设置播放时间会重复触发
  138. if (this.needSeek) {
  139. this.videoToastShow = true;
  140. setTimeout(() => {
  141. this.closeToast();
  142. }, 3000);
  143. this.$emit("studyLog");
  144. this.needSeek = false;
  145. }
  146. // if (!this.Elevideo) {
  147. // this.Elevideo = document.querySelector("video.plv-player-video");
  148. // this.Elevideo &&
  149. // this.Elevideo.addEventListener("timeupdate", this.timeupdate);
  150. // }
  151. if (!this.Elevideo) {
  152. this.Elevideo = true;
  153. tcPlayerContext.on("timeupdate", this.timeupdate);
  154. }
  155. },
  156. onSeeking() {
  157. if (!this.needSeek) {
  158. console.log("触发拖拽");
  159. this.isSeeking = true;
  160. this.$emit('isSeeking', this.isSeeking);
  161. }
  162. },
  163. onSeeked() {
  164. console.log("触发拖拽结束");
  165. this.isSeeking = false;
  166. this.$emit('isSeeking', this.isSeeking);
  167. },
  168. resumeVideo() {
  169. tcPlayerContext.play();
  170. },
  171. playerError(err) {
  172. console.log("腾讯视频播放err", err);
  173. },
  174. timeupdate(e) {
  175. if(this.isSeeking) return;
  176. let time = 0;
  177. if (!this.PlayStart) return
  178. time = this.PlayStart == 1 ? (this.currentTime || 0) : this.playCurrentTime();
  179. if (!this.isInitSeek) {
  180. if (time >= this.ct) {
  181. this.isInitSeek = true;
  182. } else {
  183. return;
  184. }
  185. }
  186. this.onVideoSeek1(time);
  187. this.PlayStart++
  188. this.$emit("timeupdate", time);
  189. },
  190. h5StateChange() {
  191. let states = {
  192. loadstart: "onPlayerInitOver", // 播放器初始化完毕时触发
  193. pause: "pause", // 视频暂停时触发
  194. playing: "playing", // 视频初次播放或由暂停恢复播放时触发
  195. seeking: "onSeeking",
  196. seeked: "onSeeked",
  197. ended: "ended", // 当前视频播放完毕时触发
  198. error: "playerError", // 播放出现错误时触发
  199. };
  200. let that = this;
  201. for (const key in states) {
  202. tcPlayerContext.on(key, function() {
  203. that[states[key]] && that[states[key]](...arguments);
  204. that.$emit(states[key]);
  205. });
  206. }
  207. },
  208. onVideoSeek(start, end, vid) {
  209. // if (this.allowSeek == "off") {
  210. // return;
  211. // }
  212. // if (end - this.playTime > 10) {
  213. // this.seekVideo(start);
  214. // }
  215. },
  216. // 新增用户视频学习日志
  217. studyLog() {
  218. // this.$http({
  219. // url: "/user/study/log",
  220. // method: "post",
  221. // data: {
  222. // goodsId: this.goodsId,
  223. // courseId: this.courseId,
  224. // moduleId: this.moduleId || 0,
  225. // chapterId: this.chapterId || 0,
  226. // sectionId: this.playSectionId || 0,
  227. // fromPlat: 1, //来源平台 1小程序 2PC网站
  228. // goodsType: 1, // 商品类型 1视频2题库 3补考 4前培 5虚拟赠送题库 6直播
  229. // orderGoodsId: this.orderGoodsId,
  230. // },
  231. // }).then((res) => {});
  232. },
  233. onVideoSeek1(time) {
  234. if (!this.controlBar) {
  235. if (Math.abs(time - this.playTime) > 5) {
  236. console.log("进度条回拉", this.playTime, this.ct);
  237. this.seekVideo(this.playTime);
  238. } else {
  239. this.playTime = time;
  240. }
  241. }
  242. },
  243. seekVideo(time) {
  244. time = time || 0;
  245. tcPlayerContext.currentTime(time);
  246. },
  247. restart() {
  248. this.seekVideo(0);
  249. this.closeToast();
  250. },
  251. // 播放时刻
  252. playCurrentTime() {
  253. if (!tcPlayerContext) {
  254. return 0;
  255. }
  256. return tcPlayerContext.currentTime();
  257. },
  258. getDuration() {
  259. return tcPlayerContext.duration();
  260. },
  261. // 本次看的时长
  262. playVideoTime() {
  263. if (!tcPlayerContext) {
  264. return 0;
  265. }
  266. return tcPlayerContext.currentTime();
  267. },
  268. // 暂停播放
  269. playPause() {
  270. if (!tcPlayerContext) {
  271. return;
  272. }
  273. tcPlayerContext.pause();
  274. },
  275. // 退出全屏
  276. exitFullScreen() {
  277. if (!tcPlayerContext) {
  278. return;
  279. }
  280. if (
  281. !!(
  282. document.webkitIsFullScreen ||
  283. document.mozFullScreen ||
  284. document.msFullscreenElement ||
  285. document.fullscreenElement
  286. )
  287. ) {
  288. try {
  289. var de = document;
  290. if (de.exitFullscreen) {
  291. de.exitFullscreen();
  292. } else if (de.mozCancelFullScreen) {
  293. de.mozCancelFullScreen();
  294. } else if (de.webkitCancelFullScreen) {
  295. de.webkitCancelFullScreen();
  296. }
  297. } catch (err) {}
  298. }
  299. },
  300. onPlayerInitOver() {
  301. console.log("腾讯视频加载完毕");
  302. uni.$on("playPause", this.playPause);
  303. this.$emit("loadedmetadata", tcPlayerContext);
  304. },
  305. onPlayStart() {
  306. console.log("腾讯视频初次播放");
  307. this.PlayStart = 1;
  308. },
  309. loadedmetadata() {
  310. if (this.hasStart) {
  311. // 防止loadedmetadata事件第二次触发
  312. return;
  313. }
  314. this.hasStart = true;
  315. setTimeout(() => {
  316. this.hasStart = false;
  317. }, 3000);
  318. this.onPlayerInitOver();
  319. },
  320. changeFileId(data) {
  321. console.log('调用腾讯视频changeFileId')
  322. if (!data) {
  323. return;
  324. }
  325. this.config = this.$method.isObject(data) ?
  326. data : {
  327. fileId: data,
  328. currentTime: this.currentTime
  329. };
  330. if (!this.fileId) {
  331. return;
  332. }
  333. this.PlayStart = 0;
  334. this.loadPlayerScript(this.loadPlayer);
  335. },
  336. closeToast() {
  337. this.videoToastShow = false;
  338. },
  339. },
  340. destroyed() {
  341. if (tcPlayerContext) {
  342. tcPlayerContext.off();
  343. tcPlayerContext.dispose();
  344. }
  345. // // #ifdef H5
  346. // this.Elevideo &&
  347. // this.Elevideo.removeEventListener("timeupdate", this.timeupdate, false);
  348. // // #endif
  349. tcPlayerContext = null;
  350. },
  351. computed: {
  352. fileId() {
  353. return this.config ? this.config.fileId : this.playFileId;
  354. },
  355. ct() {
  356. return (
  357. (this.config ? this.config.currentTime : this.currentTime) ||
  358. 0
  359. );
  360. },
  361. },
  362. watch: {
  363. ct: {
  364. handler(time) {
  365. if (time > 0) {
  366. this.needSeek = true;
  367. this.playTime = time;
  368. }
  369. },
  370. immediate: true,
  371. },
  372. },
  373. };
  374. </script>
  375. <style lang="scss" scoped>
  376. .player_box {
  377. position: relative;
  378. .video-toast {
  379. position: absolute;
  380. width: 686rpx;
  381. height: 80rpx;
  382. background: rgba(0, 0, 0, 0.6);
  383. border-radius: 24rpx;
  384. bottom: 100rpx;
  385. left: 50%;
  386. transform: translateX(-50%);
  387. color: #fff;
  388. display: flex;
  389. font-size: 26rpx;
  390. align-items: center;
  391. overflow: visible;
  392. z-index: 999;
  393. &__text {
  394. flex: 1;
  395. margin-left: 40rpx;
  396. }
  397. &__btn {
  398. width: 180rpx;
  399. text-align: center;
  400. border-left: 1rpx solid #fff;
  401. }
  402. }
  403. .video-toast__close {
  404. position: absolute;
  405. right: 32rpx;
  406. bottom: 184rpx;
  407. width: 40rpx;
  408. height: 40rpx;
  409. line-height: 40rpx;
  410. text-align: center;
  411. background: rgba(0, 0, 0, 0.6);
  412. border-radius: 50%;
  413. color: rgba(255, 255, 255, 0.3);
  414. }
  415. }
  416. </style>