vue项目中flv格式的视频流播放

<template>
    <div class="livePlayerContainer">

        <!-- LivePlayer使用 -->
        <div class="livePlayerBox" style="position: relative">
            <!-- :aspect="'16:9'" -->
            <!-- custom-buttons="极速/流畅, 拉伸/标准" -->
            <!-- :custom-buttons="getVideoLcd" -->
            
            <LivePlayer ref="player"
                        :videoTitle="titleValue"
                        :videoUrl="getFlvURL" 
                        live
                        aspect='fullscreen'
                        :loading="true"
                        :stretch="true"
                        :show-custom-button="true"
                        :resolution="'yh,hd,sd'"
                        @message="message"
                        @error="error"
                        @ended="ended"
                        @timeupdate="timeupdate"
                        @pause="pause"
                        @play="play" 
                        @fullscreen="fullscreen" 
                        @snapOutside="snapOutside"
                        @snapInside="snapInside"
                        @customButtons="customButtons" />
        </div>

        <!-- 外部按钮调用事件 -->
        <div style="display:flex;justify-content:space-between" v-if="false">
            <el-button type="success" @click="customPause">暂停</el-button>
            <el-button type="success" @click="customPlay">播放</el-button>
            <el-button type="success" @click="customSnap">截图数据</el-button>
            <el-button type="success" @click="destoryLivePlayer">销毁直播流</el-button>
        </div>
       
    </div>
</template>

<script>
import LivePlayer from '@liveqing/liveplayer'
export default {
    name: 'liveplayer',
    components: {
        LivePlayer
    },
    props: {
        sources:{
            type: [Array,String],
            required: true
        },
        title:{
            type: String,
            default:'直播流标题'
        },
        uniqueKey:{
            type: Number,
            default:-1
        }
    },
    data(){
        return {
            flvURL:null,
            changeVideoLcd:'高清'
        }
    },
    watch:{
        sources:{
            handler(nv,ov){
                if(nv){
                    let type = Array.isArray(nv)
                    switch(type){
                        case true:
                            this.flvURL = nv[0].url
                            break;
                        default:
                            this.flvURL = nv
                            break;
                    }
                   
                }
            },
            immediate:true,
            deep:true
        }
    },
    computed: {
        getFlvURL:{
            get() {
                return this.flvURL;
            },
            set(newValue) {
               console.log('newValue',newValue)
            }
            // return `${location.protocol}//${location.host}/video.flv`
        },
        getVideoLcd() {
            return this.changeVideoLcd;
        },
        titleValue() {
            return this.title;
        }
    },
    methods: {
        changeSource(){
            if(this.sources&&Array.isArray(this.sources)&&this.sources.length>1){
                if(this.changeVideoLcd=='高清'){
                    this.changeVideoLcd=='流畅'
                    this.flvURL = this.sources[1].url
                } else {
                    this.changeVideoLcd=='高清'
                    this.flvURL = this.sources[0].url
                }
            }
            
        },
        // 用户自定义点击
        destoryLivePlayer(){
            this.flvURL = null;
        },
        customPause() {
            console.log('外部调用暂停')
            this.$refs.player.pause()
        },
        customPlay() {
            console.log('外部调用播放')
            this.$refs.player.play()
        },
        customSnap() {
            console.log('外部调用快照')
            this.$refs.player.snap();
        },
        // 原本的事件
        error(error){
            // console.log('播放器出错回调',error)
        },
        message(message){
            console.log('m3u8 加载失败时触发通知消息',message)
        },
        timeupdate(timeupdate){
            // console.log('当前时间进度',timeupdate)
        },
        ended(){
            // console.log('播放结束')
        },
        pause(timestamp) {
            // console.log('暂停,参数:当前时间进度',timestamp)
        },
        play(timestamp) {
            // console.log('播放,参数:当前时间进度',timestamp)
        },
        fullscreen(flag) {
            // console.log('全屏状态',flag)
        },
        snapOutside(snapOutside) {
            // console.log('外部快照回调, 参数: 快照 Base64 数据',snapOutside)
        },
        snapInside(snapInside) {
            // console.log('内部快照回调, 参数: 快照 Base64 数据',snapInside)
        },
        customButtons(customButtons) {
            if(customButtons=='高清'||customButtons=='流畅'){
                this.changeSource()
                console.log('this.flvURL',this.flvURL,'this.changeVideoLcd',this.changeVideoLcd)
            }
            console.log('自定义按钮点击回调, 参数:名称',customButtons)
        },
    },
    beforeDestroy() {
        this.destoryLivePlayer();
        console.log('beforeDestroy',this.flvURL)
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped lang="scss">
.livePlayerContainer{
    width:100%;
    height:100%;
    .livePlayerBox{
        width:100%;
        height:100%;
    }
}
.buttonTalk{
    color:green
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值