vue+videojs九宫格切换播放(含源码)

vuecli中引入videojs进行m3u8视频播放以及分栏切换,内容涉及视频播放、宫格切换、选中窗口视频替换功能。

依赖:videojs、flv.js、videojs-contrib-hls、videojs-flvjs-es6、vue-video-player
环境:node : 14版本,可使用v14.19.1;vue2

页面效果

1.jpg

2.jpg

说明:只针对本人项目中用到的功能,代码较多

1. 示例代码运行时,请先把左边列表里面的视频地址改成自己的,代码视频链接目前为空

  1. 视频选择时判断是否有链接,无链接时走提示
  2. 根据分栏数目以及选中窗口判断视频是添加、直接替换、二次替换(涉及到窗口切换时使用)
  3. 监听视频播放状态,自定义播放出错占位;视频上遮盖蒙层进行自定义内容显示和点击事件

源码传送阵

部分代码

列表视频点击选择的视频赋值播放

// 判断分栏情况
                    // 只有一栏 ==== 没有数据进行添加,有数据进行编辑替换
                    if (this.layoutNum == 1) {
                        if (!this.playCodeList.length) {
                            this.$refs.hlsVideoPlayer.handelVideoUrl(vcode, vurl, vname, 0)
                            // 把当前的链接放进去
                            this.playCodeList.push(vcode)
                            this.playUrlList.push(vurl)
                            this.playNameList.push(vname)
                        } else {
                            // 如果已经有正在播放的视频,进行视频的替换
                            this.playCodeList = [vcode]
                            this.playUrlList = [vurl]
                            this.playNameList = [vname]
                            this.$refs.hlsVideoPlayer.editUrl(vcode, vurl, vname, 0)
                        }
                    } else {
                        // 多栏情况
                        // 还有播放窗口:当选选中窗口数大于已选中播放的列表
                        if (this.layoutNum > this.playCodeList.length) {
                            // 选中切换的是正在播放的,直接编辑替换
                            if (this.chooseIndex < this.playCodeList.length) {
                                this.$refs.hlsVideoPlayer.editIsPlay(this.chooseIndex, true)
                                this.$set(this.playCodeList, this.chooseIndex, vcode)
                                this.$set(this.playUrlList, this.chooseIndex, vurl)
                                this.$set(this.playNameList, this.chooseIndex, vname)
                                this.$refs.hlsVideoPlayer.resetEdit(vcode, vurl, vname, this.chooseIndex)
                            } else {
                                // 判断是否是已经添加过的
                                // 没有添加过,直接添加
                                if (this.chooseIndex > this.chooseMaxIndex) {
                                    this.playCodeList.push(vcode)
                                    this.playUrlList.push(vurl)
                                    this.playNameList.push(vname)
                                    this.$refs.hlsVideoPlayer.handelVideoUrl(vcode, vurl, vname)
                                } else {
                                    this.$refs.hlsVideoPlayer.editIsPlay(this.chooseIndex, true)
                                    this.$set(this.playCodeList, this.chooseIndex, vcode)
                                    this.$set(this.playUrlList, this.chooseIndex, vurl)
                                    this.$set(this.playNameList, this.chooseIndex, vname)
                                    this.$refs.hlsVideoPlayer.resetEdit(vcode, vurl, vname, this.chooseIndex)
                                }
                            }
                        } else {
                            // 添加到最后一个的时候,判断是否是从少窗口切换来的
                            this.$refs.hlsVideoPlayer.editIsPlay(this.chooseIndex, true)
                            this.$set(this.playCodeList, this.chooseIndex, vcode)
                            this.$set(this.playUrlList, this.chooseIndex, vurl)
                            this.$set(this.playNameList, this.chooseIndex, vname)
                            this.$refs.hlsVideoPlayer.resetEdit(vcode, vurl, vname, this.chooseIndex)
                        }

                        this.$refs.hlsVideoPlayer.thisCodeLength = this.playCodeList.length
                    }

分栏窗口切换

问题

如果选中切换的窗口屏幕小于当前展示的屏幕数,直接替换已经存在的,把多余的隐藏播放
说明:为什么是隐藏而不是直接销毁删除
答:直接删除会造成再次赋值视频无法播放的问题

        // 分栏管理选中改变
        changeLayout({ name, nums }) {
            // 当前分栏重复选择
            if (this.layoutName == name) return
            // 没有选中视频的时候,不允许切换分栏
            if (!this.playCodeList.length) return
            // 赋值上一个分栏窗口数,用作对比
            this.oldLayoutNum = this.layoutNum
            // 赋值页面的窗口数
            this.$refs.hlsVideoPlayer.layoutNum = nums

            let hadCodeList = []
            let hadUrlList = []
            let hadNameList = []
            // TODO 暂时没必要进行,无链接的视频添加时已进行了判断处理,此步骤无用
            this.playCodeList.forEach((item, index) => {
                // 剔除为空的情况,进行数据重新添加
                if (item != '') {
                    hadCodeList.push(item)
                    hadUrlList.push(this.playUrlList[index])
                    hadNameList.push(this.playNameList[index])
                }
            })

            // 如果选中切换的窗口屏幕小于当前展示的屏幕数,直接替换已经存在的,把多余的隐藏播放
            // 说明:为什么是隐藏而不是直接销毁删除
            // 答:直接删除会造成再次赋值视频无法播放的问题
            if (nums < this.layoutNum) {
                // 截取当前展示的前几个
                hadCodeList = hadCodeList.slice(0, nums)
                hadUrlList = hadUrlList.slice(0, nums)
                hadNameList = hadNameList.slice(0, nums)
                // 赋值当前
                this.playCodeList = [...hadCodeList]
                this.playUrlList = [...hadUrlList]
                this.playNameList = [...hadNameList]
                this.$refs.hlsVideoPlayer.thisCodeLength = hadCodeList.length

                setTimeout(() => {
                    let chooseMaxIndex = this.chooseMaxIndex
                    chooseMaxIndex = chooseMaxIndex + 1
                    if (chooseMaxIndex == 3 || chooseMaxIndex == 5 || chooseMaxIndex == 7) {
                        chooseMaxIndex = chooseMaxIndex + 1
                    } else {
                        chooseMaxIndex = chooseMaxIndex
                    }
                    for (let i = 0; i < chooseMaxIndex; i++) {
                        if (hadCodeList[i] && hadCodeList[i] != '') {
                            // 修改赋值
                            this.$refs.hlsVideoPlayer.editIsPlay(i, true)
                        } else {
                            this.$refs.hlsVideoPlayer.editIsPlay(i, false)
                        }
                    }
                }, 400)
            } else {
                this.playCodeList = hadCodeList
                this.playUrlList = hadUrlList
                this.playNameList = hadNameList
                this.$refs.hlsVideoPlayer.thisCodeLength = hadCodeList.length

                for (let i = 0; i < hadCodeList.length; i++) {
                    this.$refs.hlsVideoPlayer.editIsPlay(i, true)
                }
            }
            if (nums == this.playCodeList.length) {
                this.$refs.hlsVideoPlayer.chooseIndex = this.playCodeList.length - 1
                this.chooseIndex = this.playCodeList.length - 1
            } else {
                this.$refs.hlsVideoPlayer.chooseIndex = this.playCodeList.length
                this.chooseIndex = this.playCodeList.length
            }

            this.layoutName = name
            this.layoutNum = nums
        },
    },

链接选择替换的几种处理(子组件页面)

// 进行视频替换
        editUrl(code, src, name, index) {
            this.isCanPlay = true
            this.$set(this.urlList[index], 'isPlay', true)
            this.$set(this.urlList[index], 'isEdit', true)
            // this.$set(this.urlList[index], 'isError', false)
            this.$set(this.urlList[index], 'code', code)
            this.$set(this.urlList[index], 'url', src)
            this.$set(this.urlList[index], 'name', name)

            this.$nextTick(() => {
                this.playerList[index].src(this.makeUrlSource(src))
                this.playerList[index].load()
            })
        },
        // 编辑替换:字段设置
        editIsPlay(index, isPlay) {
            console.log(index)
            if (this.urlList[index]) {
                if (isPlay && this.urlList[index]['isEdit']) {
                    this.$set(this.urlList[index], 'isPlay', isPlay)
                } else {
                    this.$set(this.urlList[index], 'isPlay', isPlay)
                }

                if (!isPlay) {
                    this.$set(this.urlList[index], 'isEdit', false)
                }
            }
        },
        // 编辑替换:视频替换
        resetEdit(code, src, name, index) {
            if (this.urlList[index]) {
                if (this.urlList[index]['isEdit']) {
                    // 可直接编辑
                    this.editUrl(code, src, name, index)
                } else {
                    // 走销毁后添加
                    this.handelVideoUrl2(code, src, name, index)
                }
            }
        },

videojs官网:https://docs.videojs.com/
样式相关参考链接:https://blog.csdn.net/maofengwoaini/article/details/120786907
videojs事件监听参考链接:https://blog.csdn.net/zhu_zhu_xia/article/details/122577153

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容