video-tcplayer.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div class="video-container">
  3. <video ref="video" :id="videoID" :src="src" :class="className" :preload="preload" :poster="poster" playsinline webkit-playsinline :style="styleValue" />
  4. </div>
  5. </template>
  6. <script>
  7. import TCPlayer from "tcplayer.js";
  8. // import "tcplayer.js/dist/tcplayer.css";y
  9. export default {
  10. name: "video-player",
  11. props: {
  12. setting: {
  13. type: Object,
  14. default: () => {},
  15. },
  16. className: String,
  17. styleValue: String,
  18. src: String,
  19. poster: String,
  20. controls: Boolean,
  21. height: String,
  22. fullscreen: Boolean,
  23. preload: {
  24. type: String,
  25. default: "auto",
  26. },
  27. currentTime: {
  28. type: Boolean,
  29. default: true,
  30. },
  31. playLarge: {
  32. type: Boolean,
  33. default: true,
  34. },
  35. },
  36. data() {
  37. return {
  38. videoID: "video" + Date.now() + Math.floor(Math.random() * 100),
  39. player: null,
  40. };
  41. },
  42. mounted() {
  43. this.init();
  44. },
  45. updated() {
  46. this.init();
  47. },
  48. methods: {
  49. init() {
  50. const Button = TCPlayer.getComponent("Button");
  51. const BigPlayButton = TCPlayer.getComponent("BigPlayButton");
  52. BigPlayButton.prototype.createEl = function () {
  53. var el = Button.prototype.createEl.call(this);
  54. let _html =
  55. '<button><svg t="1644397862160" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3095" width="64" height="64"><path d="M298.666667 247.04V682.666667a42.666667 42.666667 0 0 1-85.333334 0V170.666667a42.666667 42.666667 0 0 1 65.024-36.352l554.666667 341.333333a42.666667 42.666667 0 0 1 0 72.704l-554.666667 341.333333a42.666667 42.666667 0 0 1-44.714666-72.704L729.258667 512 298.666667 247.04z" p-id="3096" fill="#ffffff"></path></svg></button>';
  56. el.appendChild(
  57. TCPlayer.dom.createEl("div", {
  58. className: "vjs-button-icon",
  59. innerHTML: _html,
  60. })
  61. );
  62. return el;
  63. };
  64. this.player = TCPlayer(this.videoID, {
  65. appID: "",
  66. controls: true,
  67. }); // player-container-id 为播放器容器 ID,必须与 html 中一致
  68. if (this.player) {
  69. this.player.src(this.src); // url 播放地址
  70. this.player.poster(this.poster || "");
  71. // this.player.elements.container ? (this.player.elements.container.style.height = this.height || "2rem") : null;
  72. this.player.on("play", () => {
  73. this.$emit("play");
  74. });
  75. // 初步加载时
  76. // this.player.one('loadedmetadata', () => {
  77. // console.log(' Loading metadata');
  78. // });
  79. // 视频播放时加载
  80. // this.player.on('timeupdate', () => {
  81. // });
  82. // 视频播放结束
  83. this.player.on("ended", () => {
  84. this.$emit("ended");
  85. });
  86. }
  87. },
  88. onpause() {
  89. this.player.pause();
  90. },
  91. },
  92. beforeDestroy() {
  93. if (this.player) {
  94. this.player.destroy();
  95. this.player.pause();
  96. this.player.src("");
  97. }
  98. },
  99. };
  100. </script>
  101. <style lang="less" scoped>
  102. .video-container {
  103. --plyr-color-main: #01c1b5;
  104. }
  105. .video-js {
  106. width: 100%;
  107. height: 2.1rem;
  108. overflow: hidden;
  109. }
  110. </style>