video.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div class="video-container">
  3. <video
  4. ref="video"
  5. :src="src"
  6. :class="className"
  7. :preload="preload"
  8. :poster="poster"
  9. :style="styleValue"
  10. />
  11. </div>
  12. </template>
  13. <script>
  14. import Plyr from "plyr";
  15. import "plyr/dist/plyr.css";
  16. import { browser } from "@/common/common";
  17. export default {
  18. name: "video-player",
  19. props: {
  20. setting: {
  21. type: Object,
  22. default: () => {},
  23. },
  24. className: String,
  25. styleValue: String,
  26. src: String,
  27. poster: String,
  28. controls: Boolean,
  29. height: String,
  30. fullscreen: {
  31. type: Boolean,
  32. default: true,
  33. },
  34. preload: {
  35. type: String,
  36. default: "auto",
  37. },
  38. currentTime: {
  39. type: Boolean,
  40. default: true,
  41. },
  42. playLarge: {
  43. type: Boolean,
  44. default: true,
  45. },
  46. },
  47. data() {
  48. return {
  49. player: null,
  50. };
  51. },
  52. mounted() {
  53. this.init();
  54. },
  55. updated() {
  56. this.init();
  57. },
  58. methods: {
  59. init() {
  60. let controls = ["play", "progress", "captions"];
  61. if (this.playLarge) {
  62. controls.push("play-large");
  63. }
  64. console.log(this.currentTime, "1212");
  65. if (this.currentTime) {
  66. controls.push("current-time");
  67. }
  68. if (browser().android && this.fullscreen) {
  69. controls.push("fullscreen");
  70. }
  71. this.player = new Plyr(this.$refs.video, {
  72. controls: controls,
  73. invertTime: false,
  74. ...this.setting,
  75. });
  76. this.player.elements.container
  77. ? (this.player.elements.container.style.height = this.height || "2rem")
  78. : null;
  79. this.player.on("play", () => {
  80. this.$emit("play");
  81. });
  82. },
  83. onpause() {
  84. this.player.pause();
  85. },
  86. },
  87. beforeDestroy() {
  88. // this.player.destroy();
  89. this.player.pause();
  90. this.player.src = "";
  91. this.player.restart();
  92. // window.stop();
  93. },
  94. };
  95. </script>
  96. <style lang="less" scoped>
  97. .video-container {
  98. --plyr-color-main: #01c1b5;
  99. }
  100. </style>