vue3+ts 使用vue-video-player播放本地视频

内容分享2个月前发布
0 1 0

一、安装依赖

npm install vue-video-player@5.0.2 --save
npm install video.js --save

二、main.ts引入

import VideoPlayer from  vue-video-player/src 
import  video.js/dist/video-js.css  
import  vue-video-player/src/custom-theme.css 

三、提示引入错误的解决

这时会编译错误
在文件shims-vue.d.ts(与main.ts同级),加上 declare module vue-video-player/src
没有这文件就新建

/* eslint-disable */
declare module  *.vue  {
  import type { DefineComponent } from  vue 
  const component: DefineComponent<{}, {}, any>
  export default component
}
declare module  vue-video-player/src 

四、视频封装与配置的完整代码

<template>
  <div class="video">
    <video-player class="video-player vjs-custom-skin"
                  ref="videoPlayer"
                  :playsinline="true"
                  :options="data.playerOptions">
    </video-player>
  </div>
</template>

<script>
import { reactive } from  @vue/reactivity ;

export default {
  name:  Video ,
  props:[ video , cover ],
  setup(props){
    let data = reactive({
      playerOptions: {
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选的播放速度
        autoplay: false, // 如果为true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 是否视频一结束就重新开始。
        preload:  auto , // 提议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
        language:  zh-CN ,
        aspectRatio:  4:3 , // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [{
          type: "video/mp4", // 类型
          src: props.video // url地址
        }],
        poster: props.cover, // 封面地址
        notSupportedMessage:  此视频暂无法播放,请稍后再试 , // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 是否显示全屏按钮
        }
      }
    })
    return {
      data
    }
  }
}
</script>

<style scoped>
.video{
  width: 100%;
  height: 100%;
  margin: auto;
}
</style>
<style>
.video .vjs_video_3-dimensions.vjs-fluid {
  padding-top: 57%;
}
.video .vjs-custom-skin > .video-js .vjs-big-play-button {
  /* background-color: rgba(0,0,0,0.45); */
  font-size: 3.5em;
  border-radius: 50%;
  height: 2em !important;
  width: 2em !important;
  line-height: 2em !important;
  margin-top: -1em !important;
  margin-left: -1em
}
.video .vjs-poster{
  background-position:center
}
</style>

五、如要引入本地视频文件

路径加入require()

sources: [{
          type: "video/mp4", // 类型
          src: require(props.video)
        }],

© 版权声明

相关文章

1 条评论

none
暂无评论...