vue中使用vue-video-player,播放兼容PC、移动的直播流(m3u8格式)

1,安装vue-video-player

npm install vue-video-player --S

2,在main.js里面引入

import VueVideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'

Vue.use(VideoPlayer)

以上的操作正常视频格式已经可以使用,还不可以直接使用m3u8数据流格式,以下是兼容.m3u8格式的视频操作

1,需要安装插件videojs-contrib-hls

npm install  videojs-contrib-hls -S

2,在main.js里面

import hls from 'videojs-contrib-hls'
Vue.use(hls)

使用案例:

<template>
    <div class="container">
        <div class="player">
        <video-player class="video-player vjs-custom-skin"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions"
        ></video-player>
       </div>
 </div>
</template>
 
<script>
export default {
    name: 'living',
    data () {
        return {
            playerOptions: {
                //playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
                autoplay: false, //如果true,浏览器准备好时开始回放。
                muted: false, // 默认情况下将会消除任何音频。
                loop: false, // 导致视频一结束就重新开始。
                preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
                language: 'zh-CN',
                aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
                fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
                sources: [{
                    type: "application/x-mpegURL",
                    src: "http://21810.liveplay.myqcloud.com/live/21810_ea70a9e139.m3u8" //你的m3u8地址(必填)
                }],
                poster: "poster.jpg", //你的封面地址
                 document.documentElement.clientWidth,
                notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
                //  controlBar: {
                //   timeDivider: true,
                //   durationDisplay: true,
                //   remainingTimeDisplay: false,
                //   fullscreenToggle: true //全屏按钮
                //  }
            }
        }
    },
    components: {
        videoPlayer
    },
    methods: {
        //事件
    }
}
</script>
 
<style scoped>

</style>

 注意事项:

vue-video-player 其实就是 video.js 集成到 vue 中,所以千万不要再安装 video.js,可能会出错
播放 HLS 流,需要 videojs-contrib-hls 插件,(!直接引用,因为在安装vue-video-player插件时,hls插件是一并下载下来的),如果需要 RTMP 流,需要 videojs-flash 插件,也是直接引用就可以了( flash 插件需要在 hls 之前引用)
import 'videojs-contrib-hls'
跨域可能是流地址和网站地址跨域,http和https存在跨域


原文地址:https://www.cnblogs.com/liumingwang/p/13807637.html