提交 fba2db77 作者: 翁进城

优化视频组件代码,支持品牌播放器选择

上级 05893c9a
<template> <template>
<div class="flv"> <div class="flv">
<video controls autoplay="autoplay" muted="muted" id="videoElement"> <video controls autoplay="autoplay" muted="muted" ref="video">
<source type="video/mp4" /> <source type="video/mp4" />
</video> </video>
<!-- <button @click="play">播放</button> --> <!-- <button @click="play">播放</button> -->
...@@ -17,20 +17,20 @@ export default { ...@@ -17,20 +17,20 @@ export default {
return { return {
player: null, player: null,
flvPlayer: null, flvPlayer: null,
videourl: ""
}; };
}, },
mounted() { mounted() {
this.videourl = this.data.vUrl this.init();
console.log(this.data.vUrl); },
this.ngOnInit(); beforeDestroy() {
this.destroy();
}, },
methods: { methods: {
//页面初始化 //页面初始化
ngOnInit() { init() {
if (flvjs.default.isSupported()) { if (flvjs.default.isSupported()) {
// 获取DOM对象 // 获取DOM对象
this.player = document.getElementById("videoElement"); this.player = this.$refs.video;
// 创建flvjs对象 // 创建flvjs对象
this.flvPlayer = flvjs.default.createPlayer({ this.flvPlayer = flvjs.default.createPlayer({
type: "flv", // 指定视频类型 type: "flv", // 指定视频类型
...@@ -38,21 +38,12 @@ export default { ...@@ -38,21 +38,12 @@ export default {
hasAudio: false, // 关闭声音 hasAudio: false, // 关闭声音
cors: true, // 开启跨域访问 cors: true, // 开启跨域访问
// url: "http://pzlink999bju.powzamedia.com:8000/live/flv/test/mlinkm/channel1?only-video=1", // 指定流链接 // url: "http://pzlink999bju.powzamedia.com:8000/live/flv/test/mlinkm/channel1?only-video=1", // 指定流链接
url: this.getTarget(this.videourl), // 指定流链接 url: this.data.vUrl, // 指定流链接
}); });
// 将flvjs对象和DOM对象绑定 // 将flvjs对象和DOM对象绑定
this.flvPlayer.attachMediaElement(this.player); this.flvPlayer.attachMediaElement(this.player);
} }
this.play() this.play();
},
getTarget(url) {
const reg = /^pzsp:\/\/(.*):7788.*\/trans\/(.*)\/mlinkm\/(.*)\?/;
url.match(reg);
const hostname = RegExp.$1;
const groupname = RegExp.$2;
const streamname = RegExp.$3;
return `http://${hostname}:8000/live/flv/${groupname}a/mlinkm/${streamname}?only-video=1`;
}, },
// 播放视频 // 播放视频
...@@ -62,7 +53,7 @@ export default { ...@@ -62,7 +53,7 @@ export default {
}, },
//页面退出销毁和暂停播放 //页面退出销毁和暂停播放
ngOnDestroy() { destroy() {
this.flvPlayer.pause(); this.flvPlayer.pause();
this.flvPlayer.unload(); this.flvPlayer.unload();
// 卸载DOM对象 // 卸载DOM对象
...@@ -70,6 +61,18 @@ export default { ...@@ -70,6 +61,18 @@ export default {
// 销毁flvjs对象 // 销毁flvjs对象
this.flvPlayer.destroy(); this.flvPlayer.destroy();
}, },
fullScreen() {
let video = this.$refs.video;
if (video.webkitRequestFullScreen) {
video.webkitRequestFullScreen();
} else if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen)
setTimeout(() => {
video.play();
}, 200);
},
}, },
}; };
</script> </script>
...@@ -78,7 +81,7 @@ export default { ...@@ -78,7 +81,7 @@ export default {
.flv { .flv {
width: 100%; width: 100%;
height: 100%; height: 100%;
#videoElement { video {
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
......
...@@ -113,7 +113,7 @@ export default { ...@@ -113,7 +113,7 @@ export default {
const image = canvasToDataURL(canvas); const image = canvasToDataURL(canvas);
let blob = dataURLToBlob(image); let blob = dataURLToBlob(image);
return blob; return blob;
} },
} }
}; };
</script> </script>
......
<template>
<div class="cpt-player-webrtc">
<video id="rtc_media_player" ref="webrtc" controls autoplay></video>
</div>
</template>
<script>
import $ from "./jquery-1.10.2.min";
import {
SrsRtcPublisherAsync,
SrsRtcPlayerAsync,
SrsRtcFormatSenders,
} from "./srs.sdk";
window.$ = $;
export default {
props: {
data: {
type: Object,
default: () => ({}),
},
},
data() {
return {
sdk: null,
};
},
watch: {
data: {
handler(value) {
this.$nextTick(() => {
this.init();
});
},
deep: true,
immediate: true,
},
},
beforeDestroy() {
this.sdk.close();
},
methods: {
pause() {
if (this.$refs["webrtc"]) {
this.$refs["webrtc"].pause();
}
},
play() {
if (this.$refs["webrtc"]) {
this.$refs["webrtc"].play();
}
},
init() {
let _this = this;
if (this.$refs["webrtc"]) {
if (this.sdk) {
this.sdk.close();
}
this.sdk = new SrsRtcPlayerAsync();
this.$refs["webrtc"].srcObject = this.sdk.stream;
this.sdk
.play(this.data.vUrl)
.then(function (session) {})
.catch(function (reason) {
console.log('srs err', reason)
_this.sdk.close();
});
}
},
fullScreen() {
let video = this.$refs["webrtc"];
if (video.webkitRequestFullScreen) {
video.webkitRequestFullScreen();
} else if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen)
setTimeout(() => {
video.play();
}, 200);
},
},
};
</script>
<style lang="scss" scoped>
.cpt-player-webrtc {
height: 100%;
width: 100%;
background-color: #000;
video {
width: 100%;
height: 100%;
object-fit: fill;
}
}
</style>
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
<!-- 总览页视频 -->
<template>
<div class="cpt-player-webrtc">
<video ref="webrtc" :controls="controls">
<source />
</video>
</div>
</template>
<script>
import JSWebrtc from "./jswebrtc";
export default {
props: {
controls: {
type: Boolean,
default: () => true,
},
data: {
type: Object,
default: () => ({}),
},
},
data() {
return {
player: null,
value: "",
times: 0,
};
},
watch: {
data: {
handler(value) {
if (value) {
this.$nextTick(() => {
this.player = new JSWebrtc.Player(value.vUrl, {
video: this.$refs["webrtc"],
autoplay: true,
});
});
}
},
deep: true,
immediate: true,
},
},
beforeDestroy(){
this.player?.destroy();
},
methods: {
fullScreen() {
let video = this.$refs["webrtc"];
if (video.webkitRequestFullScreen) {
video.webkitRequestFullScreen();
} else if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.mozRequestFullScreen)
setTimeout(() => {
video.play();
}, 200);
},
exit() {
let video = this.$refs["webrtc"];
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
setTimeout(() => {
video.play();
}, 200);
},
},
};
</script>
<style lang="scss" scoped>
.cpt-player-webrtc {
height: 100%;
width: 100%;
background-color: #000;
video {
width: 100%;
height: 100%;
object-fit: fill;
position: relative;
.title {
position: absolute;
font-family: MicrosoftYaHei-Bold;
font-size: 16px;
color: #69ceff;
letter-spacing: 1px;
text-align: center;
font-weight: 700;
height: 42px;
line-height: 42px;
position: relative;
box-sizing: border-box;
position: absolute;
bottom: 0;
}
}
}
</style>
\ No newline at end of file
...@@ -17,8 +17,15 @@ ...@@ -17,8 +17,15 @@
</div> </div>
<div v-show="!fullScreen" class="close" @click="onClose(data)">关闭</div> <div v-show="!fullScreen" class="close" @click="onClose(data)">关闭</div>
</div> </div>
<el-tooltip content="模式切换" placement="bottom" v-if="playerCom !== 'QingLiuPlayer' && fpvUrl.vUrl"> <el-tooltip
<div class="fpv-switch iconfont icon-moshiqiehuan modelStyle pointer" @click="onChangeFPV"></div> content="模式切换"
placement="bottom"
v-if="playerCom !== 'QingLiuPlayer' && fpvUrl.vUrl"
>
<div
class="fpv-switch iconfont icon-moshiqiehuan modelStyle pointer"
@click="onChangeFPV"
></div>
</el-tooltip> </el-tooltip>
<components <components
:is="playerCom" :is="playerCom"
...@@ -98,10 +105,15 @@ ...@@ -98,10 +105,15 @@
<div v-else class="left ml24" style="margin-left: 124px; color: #fff;"></div> <div v-else class="left ml24" style="margin-left: 124px; color: #fff;"></div>
<div class="ctrl-items"> <div class="ctrl-items">
<div class="ctrl-items-selects"> <div class="ctrl-items-selects">
<el-select class="video_type mr24" v-model="streamSelect" @change="fpvSmallWindow = true;" placeholder="切换源"> <el-select
class="video_type mr24"
v-model="streamSelect"
@change="fpvSmallWindow = true;"
placeholder="切换源"
>
<el-option <el-option
v-for="item in streamOptions" v-for="(item, i) in _streamOptions"
:key="item.value" :key="i"
:label="item.label" :label="item.label"
:value="item.value" :value="item.value"
></el-option> ></el-option>
...@@ -129,18 +141,24 @@ ...@@ -129,18 +141,24 @@
<script> <script>
// import API from "@/api"; // import API from "@/api";
import LiveNVRPlayer from "./components/livenvr/index.vue"; import LiveNVRPlayer from "./components/livenvr";
// import QingLiuPlayer from "./flv/index.vue"; // import QingLiuPlayer from "./flv/index.vue";
import QingLiuPlayer from "./components/qingliu/index.vue"; import QingLiuPlayer from "./components/qingliu";
import FLVPlayer from "./components/flv";
import SRSPlayer from "./components/srs";
import WebRtcPlayer from './components/webrtc';
import { to_moveMount } from "../../lib/to_moveMount"; import { to_moveMount } from "../../lib/to_moveMount";
import Obstacle from './components/obstacle'; import Obstacle from "./components/obstacle";
export default { export default {
name: "MMCPlayer", name: "MMCPlayer",
components: { components: {
QingLiuPlayer, QingLiuPlayer,
LiveNVRPlayer, LiveNVRPlayer,
Obstacle FLVPlayer,
SRSPlayer,
WebRtcPlayer,
Obstacle,
}, },
props: { props: {
//网络类型 //网络类型
...@@ -148,7 +166,7 @@ export default { ...@@ -148,7 +166,7 @@ export default {
type: String, type: String,
default: () => "公网", default: () => "公网",
}, },
//流可选项, {value: "播放地址", label: '流名称', fpvUrl: ''} //流可选项, {value: "播放地址", label: '流名称', fpvUrl: '', brand: 'srs|fly|liveNVR|QingLiu|webrtc'}
streamOptions: { streamOptions: {
type: Array, type: Array,
default: () => [], default: () => [],
...@@ -164,13 +182,13 @@ export default { ...@@ -164,13 +182,13 @@ export default {
}, },
obstacleData: { obstacleData: {
type: Object, type: Object,
default(){ default() {
return null; /* { return null; /* {
distances: [], //array<int>(36个值每10度一个与障碍物的距离厘米); distances: [], //array<int>(36个值每10度一个与障碍物的距离厘米);
obsDistance: 0, //int(避障警告距离米)} obsDistance: 0, //int(避障警告距离米)}
} */ } */
} },
} },
}, },
data() { data() {
return { return {
...@@ -214,12 +232,43 @@ export default { ...@@ -214,12 +232,43 @@ export default {
* 播放器组件名 * 播放器组件名
*/ */
playerCom() { playerCom() {
if (this.streamSelect?.includes("pzsp://")) { if (this.stream?.brand) {
return "QingLiuPlayer"; switch (this.stream.brand.toLowerCase()) {
case "flv":
return "FLVPlayer";
case "srs":
return "SRSPlayer";
case "livenvr":
return "LiveNVRPlayer";
case "qingliu":
return "QingLiuPlayer";
case "webrtc":
return "WebRtcPlayer";
default:
throw "不支持的播放器品牌!";
}
} else { } else {
return "LiveNVRPlayer"; if (this.vUrl.vUrl?.includes("pzsp://")) {
return "QingLiuPlayer";
} else {
return "LiveNVRPlayer";
}
} }
}, },
_streamOptions(){
return this.streamOptions.map((item, i) => {
item.value = i;
return item;
})
},
/**
* 选择流选项
*/
stream() { stream() {
let find = this.streamOptions.find((item) => { let find = this.streamOptions.find((item) => {
return item.value === this.streamSelect; return item.value === this.streamSelect;
...@@ -237,7 +286,7 @@ export default { ...@@ -237,7 +286,7 @@ export default {
*/ */
vUrl() { vUrl() {
return { return {
vUrl: this.stream?.value || "", vUrl: this.stream?.url || "",
}; };
}, },
fpvUrl() { fpvUrl() {
...@@ -248,31 +297,23 @@ export default { ...@@ -248,31 +297,23 @@ export default {
}, },
watch: { watch: {
streamOptions: { streamOptions: {
handler: function() { handler: function () {
console.log('streamOptions'); console.log("streamOptions");
//当流选项变化时,如果未选择流类型,则自动选择第一个流类型 //当流选项变化时,如果未选择流类型,则自动选择第一个流类型
if (this.streamOptions.length > 0) { if (this._streamOptions.length > 0) {
this.streamSelect = this.streamOptions[0].value; this.streamSelect = this._streamOptions[0].value;
} }
}, },
deep: true, deep: true,
immediate: true immediate: true,
}, },
}, },
mounted() { mounted() {
this.wrapCenter(); this.wrapCenter();
}, },
methods: { methods: {
test(){ test() {
alert('TEST'); alert("TEST");
},
firstCopySuccess(e) {
console.log("copy arguments e:", e);
alert("复制成功!");
},
firstCopyError(e) {
console.log("copy arguments e:", e);
alert("复制失败!");
}, },
/** /**
* 切换展示清流调试信息 * 切换展示清流调试信息
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
<script src="./js/icontfont.js"></script> <script src="./js/icontfont.js"></script>
<script type="text/javascript" src="./js/nipplejs.min.js"></script> <script type="text/javascript" src="./js/nipplejs.min.js"></script>
<script src="js/PIMediaPlayer_api.js"></script> <script src="js/PIMediaPlayer_api.js"></script>
<script src="./js/liveplayer-lib.min.js"></script>
<script src="./s3m.js"></script> <script src="./s3m.js"></script>
<script type="text/javascript" src="js/kbt_api.js"></script> <script type="text/javascript" src="js/kbt_api.js"></script>
<script src="liveplayer/liveplayer-lib.min.js"></script> <script src="liveplayer/liveplayer-lib.min.js"></script>
......
...@@ -3,7 +3,10 @@ export default ` ...@@ -3,7 +3,10 @@ export default `
1.清流需要网站响应头中添加 1.清流需要网站响应头中添加
"Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp", "Cross-Origin-Embedder-Policy": "require-corp",
2.需要将mmc-stl-vue2/components/MMCPlayer/lib/js和mmc-stl-vue2/components/MMCPlayer/lib/liveplayer复制到项目的public文件中 2.需要将mmc-stl-vue2/components/MMCPlayer/lib/js和mmc-stl-vue2/components/MMCPlayer/lib/liveplayer复制到项目的public文件中, index.html添加以下代码
<script src="js/PIMediaPlayer_api.js"></script>
<script src="js/kbt_api.js"></script>
<script src="liveplayer/liveplayer-lib.min.js"></script>
<template> <template>
<div> <div>
...@@ -22,24 +25,36 @@ export default { ...@@ -22,24 +25,36 @@ export default {
return { return {
streamOptions: [ streamOptions: [
{ {
label: "播放源1", label: "flv",
value: brand: "flv", //支持以下品牌播放器"srs|fly|liveNVR|QingLiu|webrtc", 为空则根据url判断使用liveNVR或QingLiu
url:
"https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv", "https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv",
fpvUrl:
"http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8",
}, },
{ {
label: "播放源2", label: "liveNVR",
value: brand: 'liveNVR',
url:
"http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8", "http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8",
fpvUrl: fpvUrl: //只有liveNVR支持fpvUrl
"https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv", "https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv",
}, },
{ {
label: "播放源3", label: "srs",
value: brand: 'srs',
url:
"webrtc://live.mmcuav.cn/hls/shouguang01",
},
{
label: "QingLiu",
url:
"pzsp://pzlink999bju.powzamedia.com:7788/live/ld/trans/test/mlinkm/channel1?ndselect=2&linkmode=8&fstusrd=1&us=1&wsinfo=pzlink999bju.powzamedia.com-13000&only-video=1&rct=500", "pzsp://pzlink999bju.powzamedia.com:7788/live/ld/trans/test/mlinkm/channel1?ndselect=2&linkmode=8&fstusrd=1&us=1&wsinfo=pzlink999bju.powzamedia.com-13000&only-video=1&rct=500",
}, },
{
label: "WebRtc",
brand: 'webrtc',
url:
"webrtc://live.mmcuav.cn/hls/shouguang01",
},
], ],
obstacleData: null, obstacleData: null,
/* { /* {
......
...@@ -15,24 +15,36 @@ export default { ...@@ -15,24 +15,36 @@ export default {
return { return {
streamOptions: [ streamOptions: [
{ {
label: "播放源1", label: "flv",
value: brand: "flv", //支持以下品牌播放器"srs|fly|liveNVR|QingLiu|webrtc", 为空则根据url判断使用liveNVR或QingLiu
url:
"https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv", "https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv",
fpvUrl:
"http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8",
}, },
{ {
label: "播放源2", label: "liveNVR",
value: brand: 'liveNVR',
"http://219.151.31.38/liveplay-kk.rtxapp.com/live/program/live/hnwshd/4000000/mnf.m3u8", url:
fpvUrl:
"https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv", "https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv",
fpvUrl: //只有liveNVR支持fpvUrl
"webrtc://live.mmcuav.cn/hls/shouguang01",
}, },
{ {
label: "播放源3", label: "srs",
value: brand: 'srs',
url:
"webrtc://live.mmcuav.cn/hls/shouguang01",
},
{
label: "QingLiu",
url:
"pzsp://pzlink999bju.powzamedia.com:7788/live/ld/trans/test/mlinkm/channel1?ndselect=2&linkmode=8&fstusrd=1&us=1&wsinfo=pzlink999bju.powzamedia.com-13000&only-video=1&rct=500", "pzsp://pzlink999bju.powzamedia.com:7788/live/ld/trans/test/mlinkm/channel1?ndselect=2&linkmode=8&fstusrd=1&us=1&wsinfo=pzlink999bju.powzamedia.com-13000&only-video=1&rct=500",
}, },
{
label: "WebRtc",
brand: 'webrtc',
url:
"webrtc://live.mmcuav.cn/hls/shouguang01",
},
], ],
obstacleData: null, obstacleData: null,
/* { /* {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论