player.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <div class="video-container-wrap">
  3. <div class="player">
  4. <video-player
  5. ref="videoPlayer"
  6. :playsinline="false"
  7. :options="playerOptions"
  8. class="video-player vjs-custom-skin"
  9. @play="onPlayerPlay($event)"
  10. @pause="onPlayerPause($event)"
  11. @statechanged="playerStateChanged($event)"
  12. ></video-player>
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. // 引入样式
  18. import { videoPlayer } from 'vue-video-player'
  19. import 'video.js/dist/video-js.css'
  20. export default {
  21. components: {
  22. videoPlayer
  23. },
  24. props: {
  25. videoUrl: { type: String, default: '' },
  26. state: { type: Boolean, default: false },
  27. poster: { type: String, default: '' }
  28. },
  29. data() {
  30. return {
  31. playerOptions: {
  32. autoplay: false, // 如果true,浏览器准备好时开始回放。
  33. muted: false, // 默认情况下将会消除任何音频。
  34. loop: false, // 导致视频一结束就重新开始。
  35. preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
  36. language: 'en',
  37. aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
  38. fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
  39. sources: [
  40. {
  41. type: 'video/mp4',
  42. src: this.videoUrl // 你的m3u8地址(必填)
  43. }
  44. ],
  45. poster: this.poster,
  46. // poster: 'https://surmon-china.github.io/vue-quill-editor/static/images/surmon-3.jpg', // 你的封面地址
  47. width: document.documentElement.clientWidth,
  48. notSupportedMessage: this.$t('material.noPlay') // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  49. }
  50. }
  51. },
  52. computed: {
  53. player() {
  54. return this.$refs.videoPlayer.player
  55. }
  56. },
  57. watch: {
  58. // 更改视频源 videoUrl从弹出框组件传值
  59. videoUrl(val, oldVal) {
  60. if (val === oldVal) {
  61. return
  62. }
  63. if (val !== '') {
  64. this.playerOptions.sources[0].src = val
  65. }
  66. },
  67. // 弹出框关闭后暂停 否则一直在播放 state从弹出框组件传值
  68. state(val, oldVal) {
  69. if (val === oldVal) {
  70. return
  71. }
  72. this.$refs.videoPlayer.player.pause()
  73. },
  74. poster(val, oldVal) {
  75. // 检测它的变化,来改变视频封面
  76. if (val === oldVal) {
  77. return
  78. }
  79. this.playerOptions.poster = val
  80. }
  81. },
  82. methods: {
  83. onPlayerPlay(player) {},
  84. onPlayerPause(player) {},
  85. playerStateChanged(player) {}
  86. }
  87. }
  88. </script>
  89. <style lang="scss">
  90. .video-container-wrap {
  91. .player {
  92. .video-js .vjs-big-play-button {
  93. /*
  94. 播放按钮换成圆形
  95. */
  96. height: 2em;
  97. width: 2em;
  98. line-height: 2em;
  99. border-radius: 1em;
  100. top: 50%;
  101. left: 50%;
  102. transform: translate(-50%, -50%);
  103. }
  104. }
  105. }
  106. </style>