单页面引用
import "video.js/dist/video-js.css";
import { videoPlayer } from "vue-video-player";
import VideoPlayerOption from "@/app/com/bean/VideoPlayerOption";
html
  <div class="video">
	<video-player
      class="video-player vjs-custom-skin"
      ref="videoPlayer"
      :playsinline="true"
      :options="playerOptions"
      @pause="onPlayerPause($event)"
      @play="onPlayerPlay($event)"
      @ended="onPlayerEnded($event)"
    >
    </video-player>
    <!--可以在tilt里面添加事件-->
    <div
      class="tilt" 
      @click="onPlayerClick"
    >
  </div>
css
.video-player,
.video-js {
  position: relative;
  width: 100%;
  height: 100% !important;
}

.vjs-fluid {
  padding: 0 !important;
}

.video {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 0;

  .tilt {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 3.0em; /*底部控件是3.0em*/
    z-index: 1;
  }
}
ts
  private playerOptions: VideoPlayerOption = new VideoPlayerOption();//设置
  private isPlay: boolean = false; //播放状态
  //获取player
  get player() {
    const videoPlayer: any = this.$refs.videoPlayer;
    return videoPlayer.player;
  }
  
  private onPlayerPause(e: any) {
    this.isPlay = false; //暂停
  }
  
  private onPlayerPlay(e: any) {
    this.isPlay = true; //播放
  }
  
  private onPlayerClick() {
    if (this.isPlay) {
      this.player.pause();
    } else {
      this.player.play();
    }
  }
VideoPlayerOption
export default class VideoPlayerOption {
  public playbackRates: Array<number> = [0.5, 1.0, 1.5, 2.0] // 可选的播放速度
  public autoplay: boolean = false// 如果为true,浏览器准备好时开始回放。
  public muted: boolean = false // 默认情况下将会消除任何音频。
  public loop: boolean = true // 是否视频一结束就重新开始。
  public preload: string = "auto"// 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
  public language: string = "zh-CN"
  public aspectRatio: string = "16:9" // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
  public fluid: boolean = true// 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
  public sources: Array<{ type: string, src: string }> = [
    {
      type: "video/mp4", // 类型
      src: "" // url地址
    },
  ]
  public poster: string = ""// 封面地址
  public notSupportedMessage: string = "此视频暂无法播放,请稍后再试" // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  public controlBar: object = {
    timeDivider: true, // 当前时间和持续时间的分隔符
    durationDisplay: true, // 显示持续时间
    remainingTimeDisplay: false, // 是否显示剩余时间功能
    fullscreenToggle: true // 是否显示全屏按钮
  }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐