vue视频监控 videojs 组件封装

封装组件 

<template>
	<div class="" style="width: 100%;height: 100%">
		<video ref="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered videoBox"></video>
	</div>
</template>

<script>
	import videojs from "video.js";
	import "video.js/dist/video-js.css";
	import "videojs-flvjs-es6";
	import "videojs-flash";
	// http://61.171.95.47:8081/rtp/34020000002000000002_34020000001320000001.live.flv
	export default {
		props: {
			videoUrl: {
				type: String,
				default: ""

			}
		},
		data() {
			return {
				videoPlayer: null
			};
		},
		mounted() {
			this.handelVideoUrl();
		},
		methods: {
			handelVideoUrl() {

				let options = {
					muted: true,
					controls: true, //进度条
					autoplay: true, //自动播放
					loop: true, //是否循环
					languages: {
						"zh-CN": require("video.js/dist/lang/zh-CN.json")
					},
					language: "zh-CN",
					preload: "auto",
					notSupportedMessage: '此视频暂无法播放,请稍后再试',
					textTrackDisplay: false,
				};
				const url = this.videoUrl.replace(/\s+/g, "");
				if (url.indexOf("rtmp") > -1) {
					//rtmp格式视频
					options = {
						...options,
						techOrder: ["html5", "flash"], // 兼容顺序
						sources: [{
							src: this.videoUrl,
							type: "rtmp/flv"
						}]
					};
				} else if (url.lastIndexOf(".") > -1) {
					if (url.substring(url.lastIndexOf(".") + 1) === "flv") {
						//flv格式视频
						options = {
							...options,
							techOrder: ["html5", "flvjs"], // 兼容顺序
							flvjs: {
								mediaDataSource: {
									isLive: false,
									cors: true,
									withCredentials: false
								}
							},
							sources: [{
								src: this.videoUrl,
								type: "video/x-flv"
							}]
						};
					} else if (url.substring(url.lastIndexOf(".") + 1) === "m3u8") {
						//flv格式视频
						options = {
							...options,
							// techOrder: ["html5", "flvjs"], // 兼容顺序
							// flvjs: {
							// 	mediaDataSource: {
							// 		isLive: false,
							// 		cors: true,
							// 		withCredentials: false
							// 	}
							// },
							sources: [{
								src: this.videoUrl,
								type: "application/x-mpegURL"
							}]
						};
					} else {
						//其他格式视频
						options = {
							...options,
							sources: [{
								src: this.videoUrl,
								type: "video/mp4"
							}]
						};
					}
				}

				this.videoPlayer = videojs(this.$refs.videoPlayer, {
					...options
				});
			}
		},
		destroyed() {
			if (this.videoPlayer) {
				this.videoPlayer.dispose();
			}
		}
	};
</script>

<style>
	.videoBox {
		width: 100%;
		height: 100%
	}
</style>

使用组件

1、引入


<script>
import Video from '@/components/Video'
export default {
    components: {
		Video
	},
}
</script>

2、使用

<template>

<div style="width: 32%;height: 232px;">
<Video :videoUrl="item.CameraUrl" />
</div>
</template>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值