Vue 配置vue-video-player

配置vue-video-player,实现视频流。只需 npm i vue-video-player 即可。亲测一定要使用 npm 安装,其他安装包引入不全,无法正常使用

//package.json
{
  "name": "v3demo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "core-js": "2.6.9",
    "echarts": "^4.3.0",
    "element-ui": "^2.12.0",
    "qrcode": "^1.4.1",
    "three": "^0.108.0",
    "vue": "^2.6.10",
    "vue-baidu-map": "^0.21.22",
    "vue-clipboard2": "^0.3.1",
    "vue-lazyload": "^1.3.3",
    "vue-router": "^3.0.3",
    "vue-video-player": "^5.0.2",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.11.0",
    "@vue/cli-plugin-eslint": "^3.11.0",
    "@vue/cli-service": "^3.11.0",
    "babel-eslint": "^10.0.1",
    "babel-plugin-component": "^1.1.1",
    "eslint": "^5.16.0",
    "eslint-plugin-vue": "^5.0.0",
    "vue-template-compiler": "^2.6.10"
  }
}
//main.js
import VueVideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import "videojs-flash"
import "videojs-contrib-hls"
<template>
  <div id="">
    <!-- 视频组件 -->
    <video-player
            class="video-player-box video-player vjs-custom-skin"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions"
    ></video-player>
  </div>
</template>

<script>
  export default {
    name: 'MyVideoPlayer',
    data() {
      return {
        // videojs options
        playerOptions: {
          language: 'en', //zh-CN 中文
          muted: true, //默认情况下将会消除任何音频
          autoplay: false, //如果true,浏览器准备好时开始回放
          loop: false, //视频一结束就重新开始
          playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
          preload: 'auto', //建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
          // aspectRatio: '16:9', //将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
          fluid: false, //当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器
          // techOrder: ['flash', 'html5'],      // 兼容顺序
          // flash: {hls: {withCredentials: false}},
          // html5: {hls: {withCredentials: false}},
          sources: [{
            type: "video/mp4", //这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目  视频流-'rtmp/flv'
            src: "https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm" //url地址
          }],
          // sources: [{ // 流配置,数组形式,会根据兼容顺序自动切换
          //   type: 'rtmp/hls',
          //   src: 'rtmp://192.168.1.199:10935/hls/stream_1'
          // }, {
          //   withCredentials: false,
          //   type: 'application/x-mpegURL',
          //   src: 'http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8'
          // }],
          poster: "/static/images/author.jpg", //初始化封面图片
          width: 400, //document.documentElement.clientWidth 播放器宽度
          height: 400, //document.documentElement.clientHeight 播放器高度
          notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息
          controlBar: {
            timeDivider: true,
            durationDisplay: true,
            remainingTimeDisplay: false,
            fullscreenToggle: true,  //全屏按钮
            currentTimeDisplay: false, // 当前时间
            volumeControl: false, // 声音控制键
            playToggle: false, // 暂停和播放键
            progressControl: true, // 进度条
          }
        }
      }
    },
    mounted() {
      console.log('this is current player instance object', this.player)
    },
    computed: {
      player() {
        return this.$refs.videoPlayer.player
      }
    },
    methods: {}
  }
</script>

<style scoped>
  /* 调整播放器样式 */
  /*.video-player-box >>> #vjs_video_3 {*/
  /*  display: flex !important;*/
  /*  justify-content: center !important;*/
  /*  align-items: center !important;*/
  /*}*/

  /*.video-player-box >>> .video-js .vjs-big-play-button {*/
  /*  position: relative !important;*/
  /*  top: 0 !important;*/
  /*  left: 0 !important;*/
  /*}*/
</style>
<template>
  <div id="">
    <!-- HLS视频流组件 -->
    <video-player
            ref="videoPlayer"
            class="vjs-custom-skin"
            :options="playerOptions"
            @ready="playerReadied">
    </video-player>
  </div>
</template>

<script>
  export default {
    name: 'MyVideoPlayerHLS',
    data() {
      return {
        playerOptions: {
          // videojs and plugin options
          height: '360',
          sources: [{
            withCredentials: false,
            type: "application/x-mpegURL",
            src: "https://logos-channel.scaleengine.net/logos-channel/live/biblescreen-ad-free/playlist.m3u8"
          }],
          controlBar: {
            timeDivider: false,
            durationDisplay: false
          },
          flash: { hls: { withCredentials: false }},
          html5: { hls: { withCredentials: false }},
          poster: "https://surmon-china.github.io/vue-quill-editor/static/images/surmon-5.jpg"
        }
      }
    },
    mounted() {
      console.log('this is current player instance object', this.player)
    },
    computed: {
      player() {
        return this.$refs.videoPlayer.player
      }
    },
    methods: {
      playerReadied(player) {
        var hls = player.tech({ IWillNotUseThisInPlugins: true }).hls
        player.tech_.hls.xhr.beforeRequest = function(options) {
          // console.log(options)
          return options
        }
      }
    }
  }
</script>

<style scoped>
</style>
<template>
  <div id="">
    <!-- RTMP视频流组件 -->
    <video-player
            class="vjs-custom-skin"
            :options="playerOptions">
    </video-player>
  </div>
</template>

<script>
  export default {
    name: 'MyVideoPlayerRTMP',
    data() {
      return {
        playerOptions: {
          height: '360',
          sources: [{
            type: 'rtmp/hls',
            src: 'rtmp://58.200.131.2:1935/livetv/hunantv'
          }],
          techOrder: ['flash'],
          autoplay: false,
          controls: true,
          poster: "https://surmon-china.github.io/vue-quill-editor/static/images/surmon-9.jpg"
        }
      }
    }
  }
</script>

<style scoped>
</style>

使用时,只要修改 sources 数据源和类型即可

### Vue3 中集成 `vue-video-player` 的使用教程 #### 安装配置 为了在 Vue 3 项目中集成 `vue-video-player` 插件,需先通过 npm 或 yarn 进行安装: ```bash npm install vue-video-player --save ``` 随后,在项目的入口文件 `main.js` 中引入该插件并注册全局组件[^3]。 ```javascript import { createApp } from 'vue' import App from './App.vue' // 引入 vue-video-player 组件库及其样式表 import VideoPlayer from 'vue-video-player' require('video.js/dist/video-js.css') require('vue-video-player/src/custom-theme.css') const app = createApp(App) app.use(VideoPlayer) app.mount('#app') ``` #### 示例代码 创建一个新的 `.vue` 文件用于定义视频播放器组件。在此示例中,将展示如何设置基本属性以及启用特定功能如倍速播放、默认全屏等特性[^2]。 ```html <template> <div class="player-container"> <!-- 配置 video-player --> <video-player :options="playerOptions"></video-player> </div> </template> <script> export default { name: "MyVideoPlayer", data() { return { playerOptions: { playbackRates: [0.7, 1.0, 1.5, 2.0], // 可选播放速度 autoplay: false, muted: false, loop: false, preload: 'auto', language: 'zh-CN', aspectRatio: '16:9', // 将自动调整大小以适应容器宽度 fluid: true, // 当true时,Video.js player将具有流体大小。它会根据其容器的比例进行缩放。 sources: [{ type: "video/mp4", // 类型 src: "http://vjs.zencdn.net/v/oceans.mp4" // 路径 }], poster: "/static/images/my-poster.jpg", //封面路径 notSupportedMessage: '此视频暂无法播放,请稍后再试' //允许覆盖Video.js无法播放媒体源时显示的消息。 } }; }, }; </script> <style scoped> .player-container{ width:80%; } </style> ``` 上述实例展示了如何快速搭建一个具备基础特性的视频播放页面,并且能够满足大多数场景下的需求。对于更复杂的应用场景,则可以根据官方文档进一步定制化开发[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值