vue实现监控视频直播的示例代码

要想使用videojs我们势必是需要安装videojs的, 而且在生产环境中我们也需要依赖它, 所以如下

npm:  npm install video.js -S
npm install videojs-flash videojs-contrib-hls -S

我们打开Vue工程中的主入口main.js进行引入

// 引入videojs
import Video from 'video.js';
import 'video.js/dist/video-js.css';
Vue.prototype.$video = Video;
import hls from 'videojs-contrib-hls';
Vue.use(hls); // 要播放hls流

创建监控视频九宫格

<template>
  <div class="cell">

    <div class="cell-player">
      <div :class="cellClass(i)" v-for="i in cellCount" :key="i">
        <playVideo :video="videoInfo[i]" v-if="cellCount != 6"></playVideo>
        <playVideo :video="videoInfo[i]" v-if="cellCount == 6 && i != 2 && i != 3"></playVideo>
        <template v-if="cellCount == 6 && i == 2">
          <div class="cell-player-6-2-cell">
            <playVideo :video="videoInfo[i]"></playVideo>
            <playVideo :video="videoInfo[++i]"></playVideo>
          </div>
        </template>
      </div>
    </div>
    <div class="cell-tool">
      <div class="bk-button-group">
<!--        <el-button @click="cellCount = 1" size="small">1</el-button>-->
        <el-button @click="cellCount = 4" size="small">4</el-button>
<!--        <el-button @click="cellCount = 6" size="small">6</el-button>-->
        <el-button @click="cellCount = 9" size="small">9</el-button>
<!--        <el-button @click="cellCount = 16" size="small">16</el-button>-->
      </div>
    </div>
  </div>
</template>

<script>
  import playVideo from '@/views/test/playVideo'

  export default {
    name: 'dashboard',
    components: {
      playVideo
    },
    data() {
      return {
        videoInfo: [
          {url: "", index: 0, name: "测试1"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a72s5d84ded3180d81.m3u8", index: 1, name: "测试1"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a725sd84ded3180d81.m3u8", index: 2, name: "测试2"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a7s25d84ded3180d81.m3u8", index: 3, name: "测试3"},
          {url: "http://hls01open.ys7.com/openlive/b27fa374e9d749ddb22bs4a12e843a3131.m3u8", index: 10, name: "测试4"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a7s2s5d84ded3180d8.m3u8", index: 4, name: "测试5"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a725sd84ded3180d8.m3u8", index: 5, name: "测试6"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a725sd84ded31280d8.m3u8", index: 6, name: "测试7"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a725sd84ded33180d8.m3u8", index: 7, name: "测试8"},
          {url: "http://hls01open.ys7.com/openlive/699a3134f0864d81a725sd84ded31480d8.m3u8", index: 8, name: "测试9"},

        ],
        cellCount: 9
      }
    },
    methods: {},
    computed: {
      cellClass() {
        return function (index) {
          switch (this.cellCount) {
            case 1:
              return ['cell-player-1']
            case 4:
              return ['cell-player-4']
            case 6:
              if (index == 1)
                return ['cell-player-6-1']
              if (index == 2)
                return ['cell-player-6-2']
              if (index == 3)
                return ['cell-player-6-none']
              return ['cell-player-6']
            case 9:
              return ['cell-player-9']
            case 16:
              return ['cell-player-16']
            default:
              break;
          }
        }
      },
    },
  }
</script>
<style>
  .cell-tool {
    height: 40px;
    line-height: 30px;
    padding: 0 7px;
  }
  .cell-player {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .cell-player-4 {
    width: 50%;
    height: 50% !important;
    box-sizing: border-box;
  }

  .cell-player-1 {
    width: 100%;
    box-sizing: border-box;
  }

  .cell-player-6-1 {
    width: 66.66%;
    height: 66.66% !important;
    box-sizing: border-box;
  }
  .cell-player-6-2 {
    width: 33.33%;
    height: 66.66% !important;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
  }
  .cell-player-6-none {
    display: none;
  }
  .cell-player-6-2-cell {
    width: 100%;
    height: 50% !important;
    box-sizing: border-box;
  }
  .cell-player-6 {
    width: 33.33%;
    height: 33.33% !important;
    box-sizing: border-box;
  }
  .cell-player-9 {
    width: 33.33%;
    height: 33.33% !important;
    box-sizing: border-box;
  }
  .cell-player-16 {
    width: 25%;
    height: 25% !important;
    box-sizing: border-box;
  }
  .cell {
    display: flex;
    flex-direction: column;
    height: 100%;
  }

</style>

创建视频容器

虽然是遍历视频容器组件,但是监控视频只播放第一个,所以这里创建视频容器时,需要保证容器id不一致。

<template>
<div class="player">
  <div>{{video.name}}</div>
  <video :id='"A"+video.index'
         style="width: 100%;"
         class="video-js vjs-default-skin vjs-big-play-centered  "> </video>
</div>
</template>

<script>
  export default {
    name: "playVideo",
    props: {
      video: {
        url: "",
        index:0,
      }
    },
    data() {return {}},
    mounted() {
      this.initVideoPlayer();
    },
    methods: {
      initVideoPlayer() {
        var that=this;
        var id="#A"+this.video.index;
          const currentInstance = that.$video(document.querySelector(id),{
            autoplay:true,
            controls:true
          }).src({
            src: that.video.url,
            type: 'application/x-mpegURL',
          })
      },
    }
  }
</script>
<style scoped>
  .player  {
    background-color: black;
    width: 100%;
    height: 100%;
    border: 1px solid white;
    color: white;
    text-align: center;
  }
</style>

到此这篇关于vue实现监控视频直播的示例代码的文章就介绍到这了,更多相关vue 监控视频直播内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • vue+flv.js+SpringBoot+websocket实现视频监控与回放功能

    目录 需求: 思路: 准备工作: 实现: 最后: 需求: vue+springboot的项目,需要在页面展示出海康的硬盘录像机连接的摄像头的实时监控画面以及回放功能. 之前项目里是纯前端实现视频监控和回放功能.但是有局限性.就是ip地址必须固定.新的需求里设备ip不固定.所以必须换一种思路. 通过设备的主动注册,让设备去主动连接服务器后端通过socket推流给前端实现实时监控和回放功能: 思路: 1:初始化设备.后端项目启动时就调用初始化方法.2:开启socket连接.前端页面加载时尝试连接so

  • vue-video-player实现实时视频播放方式(监控设备-rtmp流)

    监控设备播放效果如下 1.vue项目安装vue-video-player npm install vue-video-player --save 2.编写视频播放组件(放上完整的组件例子,父组件调用时给videoSrc和playerOptions.sources[0].src赋值就可以播放了,具体操作有注释) 注:style样式部分用了lang=scss,如果自己的项目没用他请用自己的方式改一下样式部分避免报错 <template> <div class="video-js&q

  • vue + typescript + video.js实现 流媒体播放 视频监控功能

    视频才用流媒体,有后台实时返回数据, 要支持flash播放, 所以需安装对应的flash插件.当视频播放时,每间隔3秒向后台发送请求供检测心跳,表明在线收看状态,需要后台持续发送视频数据. 1. yarn add video.js videojs-flash 2. 创建videp.js声明文件 3. 创建video_player.vue组件,供外部调用.源码如下 <script lang="ts"> import { Component, Emit, Prop, Vue }

  • vue实现监控视频直播的示例代码

    要想使用videojs我们势必是需要安装videojs的, 而且在生产环境中我们也需要依赖它, 所以如下 npm:  npm install video.js -S npm install videojs-flash videojs-contrib-hls -S 我们打开Vue工程中的主入口main.js进行引入 // 引入videojs import Video from 'video.js'; import 'video.js/dist/video-js.css'; Vue.prototyp

  • vue语法之拼接字符串的示例代码

    本文介绍了vue语法之拼接字符串的示例代码,分享给大家,具体如下. 先来一行代码: <div class="swiper-slide" v-for="item in message"> <img v-bind:src="['xxx(需要拼接的字符串)'+item.picurl]" alt="" width="100%" height="245" /> </d

  • vue父子组件的嵌套的示例代码

    本文介绍了vue父子组件的嵌套的示例代码,分享给大家,具体如下: 组件的注册: 先创建一个构造器 var myComponent = Vue.extend({ template: '...' }) 用Vue.component注册,将构造器用作组件(例为全局组件) Vue.component('my-component' , myComponent) 注册局部组件: var Child = Vue.extend({ /* ... */ }) var Parent = Vue.extend({ t

  • android 仿QQ动态背景、视频背景的示例代码

    本文介绍了android 仿QQ动态背景.视频背景的示例代码,分享给大家,具体如下: 效果如下: 如上图类似效果图: 1, 自定义视频类 继承VideoView public class CustomVideoView extends VideoView { public CustomVideoView(Context context) { super(context); } public CustomVideoView(Context context, AttributeSet attrs)

  • 用ES6的class模仿Vue写一个双向绑定的示例代码

    本文介绍了用ES6的class模仿Vue写一个双向绑定的示例代码,分享给大家,具体如下: 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods class TinyVue{ constructor({el, data, methods}){ this.$data = data this.$el = document.querySelector(el) this.$methods = methods // 初始化 this._com

  • Vue 实现展开折叠效果的示例代码

    本文介绍了Vue 实现展开折叠效果的示例代码,分享给大家,具体如下: 效果如见: 1.html代码 <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js文本段落展开和收拢效果</title> <script type="text/javasc

  • vue制作抓娃娃机的示例代码

    去年为联通制作双十一活动,做四个小游戏:'配对消消乐'.移动拼图.抓娃娃.倒计时. 现在先做来分享一下制作抓娃娃游戏时的经验 先上效果图 游戏规则:在指定时间内抓到上图四张卡片为挑战成功. 现在直接说游戏主要内容:娃娃滚动.爪子向下抓取.抓到卡片 废话不多说直接上代码!(此样式是根据需求而定) <!--布局样式--> <div class="game"> <!--爪子--> <div class="paw"> <

  • ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码

    安装 官网下载 http://ffmpeg.org/ 选择需要的版本 在这个网址下载ffmpeg,https://github.com/BtbN/FFmpeg-Builds/releases 将解压后得到的以下几个文件放置在E:\FFmpeg下 环境变量 此电脑--属性--高级系统设置--环境变量 在系统变量(也就是下面那一半)处找到新建,按如下所示的方法填写 再将%FFMPEG_HOME%以及%FFMPEG_HOME%\bin写入系统变量的Path中 然后一路确定即可 验证 win+R,cmd

  • vue el-upload上传文件的示例代码

    话不多说 直接上代码 <el-upload :action="actionUrl" class="avatar-uploader" :multiple="false" name="files" ref="upload" :file-list="fileList" :on-preview="handlePreview" :on-success="hand

  • vue实现菜单权限控制的示例代码

    大家在做后台管理系统时一般都会涉及到菜单的权限控制问题.当然解决问题的方法无非两种--前端控制和后端控制.我们公司这边的产品迭代速度较快,所以我们是从前端控制路由迭代到后端控制路由.下面我会分别介绍这两种方法的优缺点以及如何实现(不熟悉vue-router API的同学可以先去官网看一波API哈). 我先简单说下项目的需求:如下图所示,有一级菜单和二级菜单,然后不同的人登录进去会展示不同的菜单. 前端控制路由的思路:将所有的路由映射表都拿到前端来维护,就是我的router.js里面将所有的菜单p

随机推荐