vue播放rtmp、hls m3u8格式视频

选用Video.js作为视频播放库,如果要支持hls m3u8还需要videojs-contrib-hls组件的支持。

安装Video.js

npm install --save video.js

安装videojs-contrib-hls

npm install --save videojs-contrib-hls

创建一个vue的播放组件

src/components/VideoPlayer/index.vue

<template>
    <div>
        <video ref="videoPlayer" class="video-js"></video>
    </div>
</template>

<script>
import videojs from 'video.js'
import 'video.js/dist/video-js.css'
import 'videojs-contrib-hls'

export default {
    name: "VideoPlayer",
    props: {
        options: {
            type: Object,
            default() {
                return {};
            }
        }
    },
    data() {
        return {
            player: null
        }
    },
    mounted() {
        this.player = videojs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
            console.log('onPlayerReady', this);
        })
    },
    beforeDestroy() {
        if (this.player) {
            this.player.dispose()
        }
    }
}
</script>

在页面中引用VideoPlayer组件

<template>
    <div class="view-warp">
        <component :is="currentComponent" :options="videoOptions"></component>
    </div>
</template>

<script>
import VideoPlayer from "../../components/VideoPlayer/index.vue";
export default {
    name: "view",
    data() {
        return {
            currentComponent: null,
            videoOptions: {
                autoplay: false,
                controls: true,
                poster: '',
                sources: [
                    {
                        src: "",
                        type: "application/x-mpegURL"
                    }
                ]
			}
        }
    },
    methods: {
        loadData() {
            this.videoOptions.poster = 'http://localhost/poster.png'
            this.videoOptions.sources[0].src = 'http://localhost/index.m3u8'
            this.currentComponent = VideoPlayer
        }
    },
    created: function() {
        this.loadData()
    }
}
</script>

<style lang="scss">
    .video-js {
        width: 1024px !important;
        height: 720px !important;
    }
    .vjs-big-play-button {
        left: 50% !important;
        top: 50% !important;
        margin-left: -2em;
        margin-top: -1.3em;
    }
    .video-warp {
        display: flex;
    }
    .view-warp {
        padding: 0;
        margin: 0 auto;
        display: flex;
        .desc {
            margin-left: 10px;
        }
    }
</style>

如果要播放rtmp需要把sources.type改成rtmp/flv。

Video.js官方参考文档:https://docs.videojs.com/tutorial-vue.html

本文标题:vue播放rtmp、hls m3u8格式视频
本文地址:https://jizhong.plus/post/2020/04/vue-rtmp-hls-m3u8.html

posted on 2020-04-08 11:21  唐际忠  阅读(2559)  评论(0编辑  收藏  举报