• 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

  • 相关阅读:
    js 进度条效果
    js切换背景颜色
    div定时放大缩小
    html图片拖放
    网站滑到指定的位置给div添加动画效果
    播放视频
    git (Linux(Centos)安装及使用教程)
    PHP 预定义常量(魔术常量)
    【POJ3254】Corn Fields
    树形DP初探•总结
  • 原文地址:https://www.cnblogs.com/tangjizhong/p/12658766.html
Copyright © 2020-2023  润新知