main.vue 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <!--
  2. - Copyright (C) 2018-2019
  3. - All rights reserved, Designed By www.joolun.com
  4. 【微信消息 - 视频】
  5. jacker:
  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. <i class="el-icon-video-play" style="font-size: 40px!important;" ></i>
  17. <p>点击播放视频</p>
  18. </div>
  19. <!-- 弹窗播放 -->
  20. <el-dialog title="视频播放" :visible.sync="dialogVideo" width="40%" append-to-body @close="closeDialog">
  21. <video-player v-if="playerOptions.sources[0].src" class="video-player vjs-custom-skin" ref="videoPlayer"
  22. :playsinline="true" :options="playerOptions"
  23. @play="onPlayerPlay($event)" @pause="onPlayerPause($event)">
  24. </video-player>
  25. </el-dialog>
  26. </div>
  27. </template>
  28. <script>
  29. // 引入 videoPlayer 相关组件。教程:https://juejin.cn/post/6923056942281654285
  30. import { videoPlayer } from 'vue-video-player'
  31. require('video.js/dist/video-js.css')
  32. require('vue-video-player/src/custom-theme.css')
  33. export default {
  34. name: "wxVideoPlayer",
  35. props: {
  36. url: { // 视频地址,例如说:https://www.iocoder.cn/xxx.mp4
  37. type: String,
  38. required: true
  39. },
  40. },
  41. components: {
  42. videoPlayer
  43. },
  44. data() {
  45. return {
  46. dialogVideo:false,
  47. playerOptions: {
  48. playbackRates: [0.5, 1.0, 1.5, 2.0], // 播放速度
  49. autoplay: false, // 如果 true,浏览器准备好时开始回放。
  50. muted: false, // 默认情况下将会消除任何音频。
  51. loop: false, // 导致视频一结束就重新开始。
  52. preload: 'auto', // 建议浏览器在 <video> 加载元素后是否应该开始下载视频数据。auto 浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
  53. language: 'zh-CN',
  54. aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
  55. fluid: true, // 当true时,Video.js player 将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
  56. sources: [{
  57. type: "video/mp4",
  58. src: "" // 你的视频地址(必填)【重要】
  59. }],
  60. poster: "", // 你的封面地址
  61. width: document.documentElement.clientWidth,
  62. notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖 Video.js 无法播放媒体源时显示的默认信息。
  63. controlBar: {
  64. timeDivider: true,
  65. durationDisplay: true,
  66. remainingTimeDisplay: false,
  67. fullscreenToggle: true //全屏按钮
  68. }
  69. }
  70. }
  71. },
  72. methods: {
  73. playVideo(){
  74. this.dialogVideo = true
  75. // 设置地址
  76. this.$set(this.playerOptions.sources[0], 'src', this.url)
  77. },
  78. closeDialog(){
  79. // 暂停播放
  80. this.$refs.videoPlayer.player.pause()
  81. },
  82. onPlayerPlay(player) {
  83. },
  84. onPlayerPause(player) {
  85. },
  86. }
  87. };
  88. </script>