vue实现rtmp流播放

vue实现rtmp流播放

使用所需

  "dependencies": {
    "videojs-contrib-hls": "5.14.1",
    "videojs-contrib-hls.js": "3.2.0",
    "videojs-flash": "2.1.0",
    "e-vue-contextmenu": "^0.1.3",
  },

实现代码
我就直接把.vue文件放进来了,所使用的rtmp流来源于:点击查看
无需配置main.js,支持右键行为。

<template>
    <div class="liveView">
        <!-- 右键 -->
        <div style="height:0px;width:0px ;">
            <Dropdown>
                <DropdownMenu
                    v-show="downShow"
                    :style="{marginLeft:left+'px',textAlign:'center',marginTop:top+'px',position:'absolute',backgroundColor:'#EEEEEE',zIndex:100,borderRadius:'5px',fontWeight:'bold'}"
                    class="contextmenu"
                >
                    <DropdownItem @click.native="stopPlay(nowIndex)" name="carLatLng">
                        <Icon type="ios-locate-outline" />停止播放
                    </DropdownItem>
                    <DropdownItem @click.native="changeVideo(nowIndex)" name="carLatLng">
                        <Icon type="ios-locate-outline" />切换码流
                    </DropdownItem>
                </DropdownMenu>
            </Dropdown>
        </div>
        <!-- 顶部图标 -->
        <Row style="padding-top:0.4rem;">
            <Col span="3" offset="20">
                <img
                    @click="viewVideoOne"
                    width="20px"
                    height="20px"
                    src="../../../../assets/image/SurveillanceVideo/one.png"
                />
                <img
                    @click="viewVideoFour"
                    style="margin-left:0.75rem"
                    width="20px"
                    height="20px"
                    src="../../../../assets/image/SurveillanceVideo/four.png"
                />

                <img
                    @click="viewVideoNine"
                    style="margin-left:0.75rem"
                    width="20px"
                    height="20px"
                    src="../../../../assets/image/SurveillanceVideo/nine.png"
                />
            </Col>
        </Row>
        <!-- 视频控件 -->
        <div class="videoView">
            <Row style="height:800px;width:1400px;padding-left:20px">
                <Col :span="Aspan" v-for="(item,index) in videoList" :key="index">
                    <video-player
                        @contextmenu.prevent.native="rightClick($event,index)"
                        style="border:1px solid;"
                        :playsinline="true"
                        class="vjs-custom-skin"
                        ref="videoPlayer"
                        :options="playerOptions[index]"
                        @pause="onPlayerPause($event)"
                        @play="onPlayerPlay($event,index)"
                        @fullscreenchange="onFullscreenChange($event,index)"
                    ></video-player>
                </Col>
            </Row>
        </div>
    </div>
</template>
<script>
// import flvjs from "flv.js/dist/flv.js";
const isProduction = process.env.NODE_ENV === "production";
export default {
    name: "liveView",
    data() {
        return {
            nowIndex: "",
            top: 0, //右键弹窗顶部距离
            left: 0, //右键弹窗左部距离
            downShow: false, //右键弹框
            Aspan: "", //设置播放器的默认列大小
            videoList: [], //存储trmp URL
            playerOptions: [] //渲染vue-video-player组件使用
        };
    },
    watch: {
        // 监听data中的downShow值得更改
        downShow(value) {
            if (value) {
                document.body.addEventListener("click", this.closeMenu);
            } else {
                document.body.removeEventListener("click", this.closeMenu);
            }
        }
    },
    created() {
        this.videoList = [
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            },
            {
                url:
                    "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
            }
        ];
        if (this.videoList.length == 1) {
            this.Aspan = "24";
        } else if (this.videoList.length == 4) {
            this.Aspan = "12";
        } else if (this.videoList.length == 9) {
            this.Aspan = "8";
        }
        for (let i = 0; i < this.videoList.length; i++) {
            let arrs = {
                autoplay: true,
                controls: true,
                techOrder: ["flash", "html5"],
                aspectRatio: "16:9",
                sourceOrder: true,
                notSupportedMessage: "此视频暂无法播放,请稍后再试", //允许覆盖Video.js无法播放媒体源时显示的默认信息。
                fluid: true,
                flash: {
                    hls: { withCredentials: false },
                    swf: isProduction
                        ? "/vue-videojs-demo/static/media/video-js.swf"
                        : "/static/media/video-js.swf"
                },
                html5: { hls: { withCredentials: false } },
                sources: [
                    {
                        withCredentials: false,
                        type: "application/x-mpegURL",
                        src: this.videoList[i].url
                    }
                ],
                poster: isProduction
                    ? "/vue-videojs-demo/static/images/logo.png"
                    : "/static/images/logo.png",
                controlBar: {
                    timeDivider: false, // 时间分割线
                    durationDisplay: false, // 总时间
                    progressControl: false, // 进度条
                    customControlSpacer: false, // 未知
                    fullscreenToggle: true // 全屏
                }
            };
            this.playerOptions.push(arrs);
        }
    },

    computed: {
        player() {
            return this.$refs.videoPlayer.player;
        }
    },
    methods: {
        //停止
        stopPlay(idx) {
            console.log(idx);
            this.$refs.videoPlayer[idx].player.pause(); // 暂停
        },
        //切换码流
        changeVideo(idx) {
            console.log(idx, this.videoList[idx]);
        },
        //播放
        onPlayerPlay(player) {
            player.play();
        },
        //暂停
        onPlayerPause(player, index) {},
        //注销vue-video-player组件
        remove() {
            this.$destroy("videoPlayer");
        },
        //右键行为
        rightClick(e, index) {
            this.nowIndex = index;
            const menuMinWidth = 105;
            const offsetLeft = this.$el.getBoundingClientRect().left;
            const offsetWidth = this.$el.offsetWidth;
            const maxLeft = offsetWidth - menuMinWidth;
            const left = e.clientX - offsetLeft;
            if (left > maxLeft) {
                this.left = maxLeft;
            } else {
                this.left = left;
            }
            this.top = e.clientY - 60;
            this.downShow = true;
        },
        //关闭菜单
        closeMenu() {
            this.downShow = false;
        },
        //单个视频播放
        viewVideoOne() {
            this.videoList = [
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                }
            ];
            this.Aspan = "24";
        },
        //四个视频播放
        viewVideoFour() {
            this.videoList = [
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                }
            ];
            this.Aspan = "12";
        },
        //九个视频播放
        viewVideoNine() {
            this.videoList = [
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                },
                {
                    url:
                        "rtmp://demo.easynvr.com:17935/hls/stream_17?token=88150c90e5792fcd9242f0f7e3cdb6f3"
                }
            ];
            this.Aspan = "8";
        }
    }

    // }
};
</script>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: Vue是一种行的前端框架,它提供了强大的工具和组件,可以帮助我们构建现代化的Web应用程序。在Vue播放RTMP而不使用Flash,我们可以使用一些现代的技术和组件。 首先,我们可以使用video.js这样的HTML5播放器库来播放RTMP。video.js是一个功能强大且易于使用的库,它支持HTML5视频播放,并且可以扩展以支持不同的媒体协议,包括RTMP。我们可以使用video.js的vue插件vue-video-player来在Vue应用中轻松地集成该播放器。 其次,我们可以使用hls.js库来播放RTMP。hls.js是一个JavaScript库,它使用HTTP Live Streaming(HLS)协议来播放视频。我们可以使用vue-hls-player这样的vue插件来在Vue中使用hls.js库。这个插件提供了一个简单的组件,可以方便地集成HLS播放器,并播放RTMP。 最后,我们还可以使用DPlayer这样的开源视频播放器库来播放RTMP。DPlayer是基于HTML5的视频播放器,它使用了开源的flv.js库来支持FLV格式的视频。我们可以使用vue-dplayer这样的vue插件来在Vue应用中使用DPlayer来播放RTMP。 总之,我们可以使用video.js、hls.js或DPlayer这些HTML5播放器库来在Vue应用中播放RTMP而不使用Flash。这些库都提供了vue插件,可以轻松地与Vue应用集成。 ### 回答2: vue播放rtmp不使用flash可以采用video.js配合Hls.js插件实现。 首先在vue项目中安装video.js和hls.js插件: ```bash npm install video.js hls.js ``` 然后在组件中引入video.js的css和js文件,并创建一个video标签: ```html <template> <div> <video id="my-video" class="video-js"></video> </div> </template> <script> import videojs from 'video.js'; import 'video.js/dist/video-js.css'; import Hls from 'hls.js'; export default { mounted() { this.initPlayer(); }, methods: { initPlayer() { const options = { autoplay: true, controls: true, sources: [{ src: 'rtmp://your-rtmp-stream-url.com/live/stream-name', type: 'rtmp/mp4', }], }; const player = videojs('my-video', options); if (Hls.isSupported()) { const hls = new Hls(); hls.loadSource('http://your-hls-stream-url.com/live/stream-name.m3u8'); hls.attachMedia(player); hls.on(Hls.Events.MANIFEST_PARSED, () => { player.play(); }); } else if (player.canPlayType('rtmp/mp4')) { player.src(options.sources[0].src); player.play(); } } } } </script> ``` 以上代码在组件加载时初始化了video.js播放器,并通过options设置播放器的参数,其中source中的src填入你的rtmp链接。然后通过Hls.js加载hls,如果浏览器不支持hls,则fallback到rtmp播放。 最后通过mounte钩子函数调用initPlayer函数,实现播放rtmp不使用flash的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值