vue实现视频流播放 vue-video-player组件

vue-video-player组件

因为要在页面显示监控实时情况,所以百度了一下,最后发现了GitHub上的一个开源项目vue-video-player的组件

1.安装vue-vide-player 和 videojs-flash

在npm下安装vue-vide-player 和 videojs-flash

npm install vue-video-player videojs-flash --save

2.引入并使用

在main.js中引用

import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
Vue.use(VideoPlayer)

在子组件使用也要引入

  import 'video.js/dist/video-js.css'
  import 'vue-video-player/src/custom-theme.css'
  import 'videojs-contrib-hls'
  import 'video.js/dist/video-js.css'
  import { videoPlayer } from 'vue-video-player'
  export default {
    components: {
      videoPlayer
    },

3.页面代码

<<template>
    <div class="liveView">
      <video-player class="vjs-custom-skin"
                    ref="videoPlayer"
                    :options="playerOptions"
                    @ready="onPlayerReadied"
                    @timeupdate="onTimeupdate"
                    :playsinline="playsinline">
      </video-player>
    </div>
</template>

<script>
  import 'video.js/dist/video-js.css'
  import 'vue-video-player/src/custom-theme.css'
  import 'videojs-contrib-hls'
  import 'video.js/dist/video-js.css'
  import { videoPlayer } from 'vue-video-player'
  export default {
    components: {
      videoPlayer
    },
    name: 'video',
    data() {
      return {
        initialized: false,
        currentTech: '',
        streams: {
          rtmp: '',
          hls: ''
        },
        playerOptions: {
          overNative: true,
          autoplay: false,
          controls: true,
          techOrder: ['html5'],
          sourceOrder: true,
          flash: {
            hls: { withCredentials: false }
          },
          html5: { hls: { withCredentials: false } },
          fluid: true,//当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
          muted: false,// 默认情况下将会消除任何音频。
          sources: [
            {
              withCredentials: false,
              type: 'application/x-mpegURL',
              src: ''
            }
          ],
          notSupportedMessage: '此视频暂无法播放,请稍后再试' //允许覆盖Video.js无法播放媒体源时显示的默认信息。,
        }
      }
    },
    computed: {
      player () {
        return this.$refs.videoPlayer.player
      },
      currentStream () {
        return this.currentTech === 'Flash' ? 'RTMP' : 'HLS'
      },
      playsinline () {
        let ua = navigator.userAgent.toLocaleLowerCase()
        // x5内核
        if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
          return false
        } else {
          // ios端
          return true
        }
      }
    },
      methods: {
        onPlayerReadied () {
          if (!this.initialized) {
            this.initialized = true
            this.currentTech = this.player.techName_
          }
        },
        // record current time
        onTimeupdate (e) {
          console.log('currentTime', e.cache_.currentTime)
        },
      }
  }
</script>

以上就可以简单的在vue中使用vue-video-player组件

4.样式 参数

在页面中data中配置可以改变组件的样式 参数 根据自身需求在date中添加

playerOptions: {
// playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
autoplay: false, // 如果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: [
{
type: 'video/mp4', // 这里的种类支持很多种:基本视频格式、直播、流媒体等,具体可以参看git网址项目
src: 'https://cdn.letvcdn.com/2018/12/05/JOCeEEUuoteFrjCg/playlist.m3u8' // url地址 替换为你的即可
}
],
hls: true,
poster: 'http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg', // 你的封面地址
width: document.documentElement.clientWidth, // 播放器宽度
notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true // 全屏按钮
}
},

以上就是本文的全部内容,希望对大家的学习有所帮助,

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值