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", //你的封面地址
                width: 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存在跨域



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM