main.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!--
  2. - Copyright (C) 2018-2019
  3. - All rights reserved, Designed By www.joolun.com
  4. 【微信消息 - 视频】
  5. 芋道源码:
  6. ① bug 修复:
  7. 1)joolun 的做法:使用 mediaId 从微信公众号,下载对应的 mp4 素材,从而播放内容;
  8. 存在的问题:mediaId 有效期是 3 天,超过时间后无法播放
  9. 2)重构后的做法:后端接收到微信公众号的视频消息后,将视频消息的 media_id 的文件内容保存到文件服务器中,这样前端可以直接使用 URL 播放。
  10. ② 体验优化:弹窗关闭后,自动暂停视频的播放
  11. -->
  12. <template>
  13. <div>
  14. <!-- 提示 -->
  15. <div @click="playVideo()">
  16. <el-icon>
  17. <VideoPlay />
  18. </el-icon>
  19. <p>点击播放视频</p>
  20. </div>
  21. <!-- 弹窗播放 -->
  22. <el-dialog
  23. title="视频播放"
  24. v-model:visible="dialogVideo"
  25. width="40%"
  26. append-to-body
  27. @close="closeDialog"
  28. >
  29. <video-player
  30. v-if="playerOptions.sources[0].src"
  31. class="video-player vjs-custom-skin"
  32. ref="videoPlayerRef"
  33. :playsinline="true"
  34. :options="playerOptions"
  35. @play="onPlayerPlay($event)"
  36. @pause="onPlayerPause($event)"
  37. />
  38. </el-dialog>
  39. </div>
  40. </template>
  41. <script setup lang="ts" name="WxVideoPlayer">
  42. // 引入 videoPlayer 相关组件。教程:https://juejin.cn/post/6923056942281654285
  43. import { videoPlayer } from 'vue-video-player'
  44. import 'video.js/dist/video-js.css'
  45. import 'vue-video-player/src/custom-theme.css'
  46. import { VideoPlay } from '@element-plus/icons-vue'
  47. const props = defineProps({
  48. url: {
  49. // 视频地址,例如说:https://www.iocoder.cn/xxx.mp4
  50. type: String,
  51. required: true
  52. }
  53. })
  54. const videoPlayerRef = ref()
  55. const dialogVideo = ref(false)
  56. const playerOptions = reactive({
  57. playbackRates: [0.5, 1.0, 1.5, 2.0], // 播放速度
  58. autoplay: false, // 如果 true,浏览器准备好时开始回放。
  59. muted: false, // 默认情况下将会消除任何音频。
  60. loop: false, // 导致视频一结束就重新开始。
  61. preload: 'auto', // 建议浏览器在 <video> 加载元素后是否应该开始下载视频数据。auto 浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
  62. language: 'zh-CN',
  63. aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
  64. fluid: true, // 当true时,Video.js player 将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
  65. sources: [
  66. {
  67. type: 'video/mp4',
  68. src: '' // 你的视频地址(必填)【重要】
  69. }
  70. ],
  71. poster: '', // 你的封面地址
  72. width: document.documentElement.clientWidth,
  73. notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖 Video.js 无法播放媒体源时显示的默认信息。
  74. controlBar: {
  75. timeDivider: true,
  76. durationDisplay: true,
  77. remainingTimeDisplay: false,
  78. fullscreenToggle: true //全屏按钮
  79. }
  80. })
  81. const playVideo = () => {
  82. dialogVideo.value = true
  83. playerOptions.sources[0].src = props.url
  84. }
  85. const closeDialog = () => {
  86. // 暂停播放
  87. // videoPlayerRef.player.pause()
  88. }
  89. // onPlayerPlay(player) {},
  90. // // // eslint-disable-next-line @typescript-eslint/no-unused-vars
  91. // // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
  92. // onPlayerPause(player) {}
  93. // methods: {
  94. // playVideo() {
  95. // this.dialogVideo = true
  96. // // 设置地址
  97. // this.playerOptions.sources[0]['src'] = this.url
  98. // },
  99. // closeDialog() {
  100. // // 暂停播放
  101. // this.$refs.videoPlayer.player.pause()
  102. // },
  103. //
  104. // //todo player组件引入可能有问题
  105. //
  106. // // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
  107. // onPlayerPlay(player) {},
  108. // // // eslint-disable-next-line @typescript-eslint/no-unused-vars
  109. // // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
  110. // onPlayerPause(player) {}
  111. // }
  112. </script>