vue-video-player實現(xiàn)實時視頻播放方式(監(jiān)控設備-rtmp流)
監(jiān)控設備播放效果如下

1、vue項目安裝vue-video-player
npm install vue-video-player --save
2、編寫視頻播放組件(放上完整的組件例子,父組件調(diào)用時給videoSrc和playerOptions.sources[0].src賦值就可以播放了,具體操作有注釋)
注:style樣式部分用了lang=scss,如果自己的項目沒用他請用自己的方式改一下樣式部分避免報錯
<template>
<div class="video-js">
<div v-if="videoSrc===''" class="no-video">
暫未播放視頻
</div>
<video-player v-else class="video-player vjs-custom-skin"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions">
</video-player>
</div>
</template>
<script>
import videojs from 'video.js'
import 'video.js/dist/video-js.css'
import 'vue-video-player/src/custom-theme.css'
import {videoPlayer} from 'vue-video-player'
import 'videojs-flash'
import SWF_URL from 'videojs-swf/dist/video-js.swf'
videojs.options.flash.swf = SWF_URL // 設置flash路徑,Video.js會在不支持html5的瀏覽中使用flash播放視頻文件
export default {
name: 'videojs',
components: {
videoPlayer
},
data () {
return {
videoSrc: '',
playerOptions: {
live: true,
autoplay: true, // 如果true,瀏覽器準備好時開始播放
muted: false, // 默認情況下將會消除任何音頻
loop: false, // 是否視頻一結(jié)束就重新開始
preload: 'auto', // 建議瀏覽器在<video>加載元素后是否應該開始下載視頻數(shù)據(jù)。auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)
aspectRatio: '16:9', // 將播放器置于流暢模式,并在計算播放器的動態(tài)大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數(shù)字(例如"16:9"或"4:3")
fluid: true, // 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。
controlBar: {
timeDivider: false,
durationDisplay: false,
remainingTimeDisplay: false,
currentTimeDisplay: false, // 當前時間
volumeControl: false, // 聲音控制鍵
playToggle: false, // 暫停和播放鍵
progressControl: false, // 進度條
fullscreenToggle: true // 全屏按鈕
},
techOrder: ['flash'], // 兼容順序
flash: {
hls: {
withCredentials: false
},
swf: SWF_URL
},
sources: [{
type: 'rtmp/flv',
src: '' // 視頻地址-改變它的值播放的視頻會改變
}],
notSupportedMessage: '此視頻暫無法播放,請稍后再試' // 允許覆蓋Video.js無法播放媒體源時顯示的默認信息。
}
}
}
}
</script>
<style scoped lang="less">
.video-js{
width:100%;
height:100%;
.no-video{
display:flex;
height:100%;
font-size:14px;
text-align:center;
justify-content: center;
align-items:center;
}
}
</style>
3、父組件調(diào)用視頻播放組件,點擊“播放視頻”替換組件里的視頻流地址播放實時視頻
<template>
<div class="about">
<video-player ref="playerObj"></video-player>
<a @click="playVideo">播放視頻</a>
</div>
</template>
<script>
import VideoPlayer from './../../components/VideoPlayer'
export default {
name: 'about',
components: {
VideoPlayer
},
data() {
return {}
},
methods: {
playVideo() {
this.$refs['playerObj'].videoSrc = 'rtmp://xxxxxxxx'
this.$refs['playerObj'].playerOptions.sources[0].src = 'rtmp://xxxxxxxx'
}
}
}
</script>
4、vue.config.js文件如下:需要加入的是chainwebpack配置
// vue.config.js
const path = require('path')
const webpack = require('webpack')
module.exports = {
baseUrl: process.env.NODE_ENV === 'production' ? '/bcmp-web/' : '/',
outputDir: process.env.NODE_ENV === 'production' ? 'bcmp-web' : 'dist',
lintOnSave: true,
productionSourceMap: false,
devServer: {
open: true,
host: '0.0.0.0',
port: 9005,
https: false,
hotOnly: false,
proxy: null
},
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
'windows.jQuery': 'jquery'
})
]
},
chainWebpack: config => {
config.module
.rule('swf')
.test(/\.swf$/)
.use('url-loader')
.loader('url-loader')
.options({
limit: 10000
})
},
pluginOptions: {
'style-resources-loader': {
preProcessor: 'scss',
patterns: [
path.resolve(__dirname, './src/assets/baseStyle/var.scss'),
path.resolve(__dirname, './src/assets/baseStyle/mixin.scss')
]
}
}
}
目前vue-video-player版本5.0.2,測試可用
補充知識:vue項目接入視頻監(jiān)控系列-------播放器的選擇
在智慧城市發(fā)展迅速的今天,視頻監(jiān)控接入web平臺的需求似乎成了不可或缺和潮流。博主準備對自己開發(fā)視頻監(jiān)控項目的經(jīng)歷做個記錄,整理成一個系列的文章。
在前端發(fā)展迅速的今天,h5的出現(xiàn)讓在web平臺實現(xiàn)無插件播放似乎成了可能,但是video對于RTMP或者RTSP協(xié)議的視頻流卻無能為力,在這里向大家推薦一個播放器: LivePlayer,這是一家視頻公司封裝的一個播放器,可以免費使用:說明文檔
(獲取的播放地址為后端配置服務后調(diào)用接口獲取的)
使用:
第一步: 安裝:
npm install @liveqing/liveplayer
npm i -D copy-webpack-plugin
第二步:引入:
在webpack.dev.conf.js中引入和聲明插件:
const CopyWebpackPlugin = require('copy-webpack-plugin')
在該文件夾下plugins中聲明插件new CopyWebpackPlugin
plugins: [
new CopyWebpackPlugin([
{ from: 'node_modules/@liveqing/liveplayer/dist/component/crossdomain.xml'},
{ from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer.swf'},
{ from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer-lib.min.js', to: 'js/'}
])]
第三步:
在index.html中引入:<script type="text/javascript" src="./js/liveplayer-lib.min.js"></script>
路徑中的js為上面輸出的js地址
第四步:
引入使用組件:
<template>
<div class="video">
<el-button type="primary" size="mini" @click="getDeviceChanleData" icon="el-icon-search">選擇通道</el-button>
<el-button type="primary" size="mini" @click="doStart" icon="el-icon-search">開始直播</el-button>
<live-player :videoUrl="videoUrl" fluent autoplay live stretch></live-player>
</div>
</template>
<script>
import LivePlayer from '@liveqing/liveplayer'
import {
getDeviceList,
getDeviceChanleList,
start
} from './apis/index.js'
export default {
data() {
return {
id: '',
videoUrl: ''
}
},
components: {
LivePlayer
},
created() {
this.getDeviceData()
},
methods: {
// 獲取設備數(shù)據(jù)列表
getDeviceData() {
const para = {
start: 1,
limit: 10,
online: true,
q: ''
}
var par = {
params: para
}
getDeviceList(par).then(res => {
console.log('設備數(shù)據(jù)', res)
this.id = res.DeviceList[0].ID
})
},
// 查詢設備通道列表
getDeviceChanleData() {
const para = {
serial: this.id
}
var par = {
params: para
}
getDeviceChanleList(par).then(res => {
console.log('設備通道數(shù)據(jù)', res)
})
},
// 開始直播
doStart() {
const para = {
serial: this.id
}
var par = {
params: para
}
start(par).then(res => {
console.log('開始直播', res)
this.videoUrl = res.RTMP
// this.videoUrl = res.HLS
// this.videoUrl = res.FLV
})
}
}
}
</script>
<style scoped>
.video{
position: relative;
width:500px;
height:300px;
}
img{
width:100%;
height:100%;
}
.time1{
position: absolute;
top:13px;
right:150px;
}
</style>
效果圖:

以上這篇vue-video-player實現(xiàn)實時視頻播放方式(監(jiān)控設備-rtmp流)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實例
今天小編就為大家分享一篇vue新vue-cli3環(huán)境配置和模擬json數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue3中使用viewerjs實現(xiàn)圖片預覽效果的項目實踐
viewer.js是一款開源的圖片預覽插件,功能十分強大,本文主要介紹了vue3中使用viewerjs實現(xiàn)圖片預覽效果的項目實踐,具有一定的參考價值,感興趣的可以了解一下2023-09-09
vue3.0+vite2實現(xiàn)動態(tài)異步組件懶加載
學了vue寫項目這么久,忽然發(fā)現(xiàn)路由懶加載的寫法,節(jié)省了加載所有路由的時間。本文主要介紹了vue3.0+vite2實現(xiàn)動態(tài)異步組件懶加載,感興趣的可以了解一下2021-06-06

