• 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 {
             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://dev-tang.com/post/2020/04/vue-rtmp-hls-m3u8.html

  • 相关阅读:
    Android Studio教程-创建第一个项目Hello World
    java打jar包的几种方式详解
    低学历者如何逆袭
    strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(MSDN的官方示例)
    select, poll, epoll
    KVM虚拟化知识的一些笔记
    在Windows系统上以C++打印出当前活动用户的环境变量
    日志组件解析
    Angular2案例rebirth开源
    视图
  • 原文地址:https://www.cnblogs.com/tangjizhong/p/12658766.html
Copyright © 2020-2023  润新知