页面视频播放

175 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第3天,点击查看活动详情

前言

前几天公司看上了人家的页面,非要照着画一个,就是一个页面上边放着一个视频,视频上方遮盖一层文字。

video.js简单尝试

这个插件是我们在网上最常见的,而且也是很好用的。

index.html:

<link href="//vjs.zencdn.net/7.10.2/video-js.min.css" rel="stylesheet">
<script src="//vjs.zencdn.net/7.10.2/video.min.js"></script>

页面中使用方式如下(这里是使用mp4格式):

test.vue:

<template>
<div>
<div class="top_video">
  <div class="left_title">
    title
  </div>
  <video class="position-relative">
    <source src="" type="video/mp4">
  </video>
</div>
  </div>
  </template>

这样我们的视频就能展示出来了。需要注意两点:

  1. 页面的视频最好是绝对定位的,这样利于你文字与视频分层。
  2. 视频的宽度有时候在css里面写不起效果,建议直接写在video标签里
  3. 我们的视频在用的时候会多出一条黑线解决方式是通过object-fit属性修改
style="width: 1900px;height: 600px;object-fit: fill;" 

object-fit CSS 属性指定可替换元素(例如:<img> 或 <video>)的内容应该如何适应到其使用高度和宽度确定的框。

有兴趣的同学可以去下面的mdn去试试

developer.mozilla.org/zh-CN/docs/…

1669452991675.png

vue-video-player

这个插件在vue中是非常常见的,而且他也是特别好用的,具体使用方式如下:

下载地址如下:

npm install vue-video-player –s

在组件中进行基本定义

video-palyer.vue

<template>
  <div class="video-dialog-container">
    <i class="el-icon-close video-dialog-close" @click="stopPlay"></i>
    <div class="video-play" v-if="dialog.show">
      <video-player class="video-player vjs-custom-skin"
                    ref="videoPlayer"
                    :playsinline="true"
                    :options="playerOptions">
      </video-player>
    </div>
  </div>
</template>
<script>
import { videoPlayer } from 'vue-video-player'
export default {
  components: {
      videoPlayer
  },
  props: {
    dialog: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      playerOptions: {
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选的播放速度
        autoplay: true, // 如果为true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 是否视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [],
        poster: '', // 封面地址
        notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: false, // 是否显示剩余时间功能
          fullscreenToggle: true // 是否显示全屏按钮
        }
      }
    }
  },
  created () {
    this.playerOptions.sources = [{
      type: 'video/mp4',
      src: this.dialog.videoUrl // url地址
    }]
    this.playerOptions.poster = this.dialog.videoImg
  },
  methods: {
    stopPlay () {
      try {
        this.$refs.videoPlayer.player.pause()
      } catch (err) {
        // console.log(err);
      }
      this.dialog.show = false;
      this.$emit('startSwiper');
    }
  }
}
</script>
<style lang="scss">
  .video-dialog-container {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    z-index: 1001;
    text-align: center;
    background: rgba(0, 0, 0, 0.6);

    .video-play {
      width: 700px;
      height: 463px;
      margin: 0 auto;
      position: absolute;
      left: 50%;
      top: 60%;
      transform: translate(-50%, -50%);
    }

    .video-dialog-close {
      position: absolute;
      right: 30px;
      top: 70px;
      width: 32px;
      height: 32px;
      font-size: 32px;
      color: #fff;
      cursor: pointer;

      &:hover {
        color: #8B9BE1;
      }
    }

    .vjs-custom-skin > .video-js .vjs-big-play-button {
      display: none;
    }
  }
</style>

在页面中使用

<template>
  <div class="page-nothome">
    <!--视频开始播放前给视频添加一个背景图-->
    <div id="video" :style="{backgroundImage:`url(${(baseURL.picAddr)+backimg})`}">
      <img @click="videoPlay()" src="../../../public/static/img/video-play.png" alt="video-play" height="84"/>
    </div>
    <!--视频播放-->
    <video-play :dialog="videoDialog" v-if="videoDialog.show"></video-play>
  </div>
</template>

<script>
import mixin from '@/mixins/index'
import videoPlay from '@/components/videoPlay';//引入定义的组件
export default {
  data () {
    return {
      videoDialog: {
        show: false,
        videoImg: '',
        videoUrl: '',
        title: ''
      },
      videourl:"",
      backimg:"",
    }
  },
  mixins: [mixin],
  components: {
    videoPlay
  },
  created () {
    this.getInitData()
    this.getTabs()
  },
  methods: {
    getInitData() {
      const pageId = this.getPageId(this.pageUrl)
      this.$http({
        url: `xxxxxxxxxx`,
        method: "GET",
        data: {}
      }).then(res => {
        let pageInfo = JSON.parse(res.data)
        this.videourl=this.baseURL.picAddr+pageInfo[0].content_info[0].content_video_url;
        this.backimg=pageInfo[0].content_info[0].content_bg_imgurl;
        this.banner = pageInfo[0].content_info[0]
        this.footerBanner = pageInfo[2].content_info[0]
      })
    },
    // 视频播放
    videoPlay(){
      this.videoDialog.show = true;
      this.videoDialog.videoImg = '/static/img/vediobg.svg';
      this.videoDialog.videoUrl = this.videourl;
      this.videoDialog.title = '公司介绍';
    }
  },
}
</script>

<style scoped lang="scss">
#video{
  height: 372px;
  text-align: center;
  width: 1024px;
  text-align: center;
  line-height: 372px;
  margin: 88px auto;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  img{
    cursor: pointer;
  }
}
</style>