videojs增加视频源选择框小工具

一、前言

在使用 video.js 开发视频播放功能时,我们经常需要根据不同网络环境或视频源,提供"多清晰度切换"功能(例如:720P、1080P、4K 等)。

虽然 video.js 提供了插件机制,但它本身没有自带清晰度切换逻辑,因此我们可以 通过继承组件的方式自定义一个清晰度下拉按钮,实现动态切换视频源的能力。

本文将结合完整源码,讲解实现思路与关键细节。


二、核心思路

整个功能的实现分为三个主要部分:

  1. 初始化播放器 :创建 video.js 实例并配置视频源;

  2. 自定义组件 QualityDropdown :继承 video.js 的 Button,实现一个带下拉菜单的按钮;

  3. 事件与逻辑控制:处理点击、菜单显示/隐藏、视频源切换等逻辑。

三、源码

复制代码
const initVideo = () => {
  const url = result.value.videoUrl;

  // 初始化播放器
  player.value = videojs(
    videoPlayer.value,
    {
      controls: true,
      autoplay: false,
      preload: 'auto',
      fluid: false,
      responsive: true,
      width: '100%',
      height: '100%',
      sources: [{ src: url, type: getVideoType(url) }],
    },
    () => {
      const videoPlayerInstance = player.value;

      // ---------- 添加清晰度切换按钮 ----------
      const Button = videojs.getComponent('Button');

      class QualityDropdown extends Button {
        constructor(player, options) {
          super(player, options);
          this.controlText('清晰度');
          this.sources = options.sources || [];
          this.current = 0;

          this.el().classList.add('vjs-quality-button');
          this.el().innerText = this.sources[this.current]?.label || '清晰度';

          // 创建下拉菜单
          this.menu = document.createElement('div');
          this.menu.className = 'vjs-quality-menu';
          this.menu.style.display = 'none';
          this.menu.style.position = 'absolute';
          this.menu.style.background = 'rgba(0,0,0,0.8)';
          this.menu.style.color = '#fff';
          this.menu.style.padding = '5px 0';
          this.menu.style.borderRadius = '4px';
          this.menu.style.top = '-70px';
          this.menu.style.left = '0';
          this.menu.style.zIndex = '1000';
          this.el().appendChild(this.menu);

          this.sources.forEach((source, index) => {
            const item = document.createElement('div');
            item.innerText = source.label;
            item.style.padding = '5px 10px';
            item.style.cursor = 'pointer';
            item.onmouseenter = () => (item.style.background = 'rgba(255,255,255,0.2)');
            item.onmouseleave = () => (item.style.background = 'transparent');
            item.onclick = () => {
              this.current = index;
              this.player().src(source);
              this.player().play();
              this.el().firstChild.nodeValue = source.label;
              this.menu.style.display = 'none';
            };
            this.menu.appendChild(item);
          });

          // 点击按钮显示/隐藏菜单
          this.el().onclick = (e) => {
            e.stopPropagation();
            this.menu.style.display = this.menu.style.display === 'none' ? 'block' : 'none';
          };

          // 点击页面其他地方隐藏菜单
          document.addEventListener('click', () => {
            this.menu.style.display = 'none';
          });
        }
      }
      // ---------- 添加清晰度切换按钮 ----------
      videojs.registerComponent('QualityDropdown', QualityDropdown);

      const qualityBtn = videoPlayerInstance.addChild('QualityDropdown', { sources: result.value.videoSources });

      // 插入到控制条末尾(进度条后面)
      const controlBarChildren = videoPlayerInstance.controlBar.children();
      videoPlayerInstance.controlBar.addChild(qualityBtn, {}, controlBarChildren.length - 1);

      // ---------- 原有事件监听 ----------
      videoPlayerInstance.on('timeupdate', () => {
        const currentTimeInSeconds = Math.floor(videoPlayerInstance.currentTime());
        updatePositionByTime(currentTimeInSeconds);
        console.log(`当前时间: ${videoPlayerInstance.currentTime().toFixed(2)}秒 / 总时长: ${videoPlayerInstance.duration().toFixed(2)}秒`);
      });

      videoPlayerInstance.on('pause', () => console.log('视频已暂停'));
      videoPlayerInstance.on('seeking', () => console.log('正在跳转到新位置...'));
      videoPlayerInstance.on('seeked', () => console.log('跳转完成,当前时间:', videoPlayerInstance.currentTime()));
      videoPlayerInstance.on('loadedmetadata', () => {
        console.log('视频元数据加载完成');
        videoPlayerInstance.dimensions(videoPlayerInstance.width(), videoPlayerInstance.height());
      });

      window.addEventListener('resize', () => {
        videoPlayerInstance.dimensions(videoPlayerInstance.width(), videoPlayerInstance.height());
      });
    }
  );
};

四、关键实现解析

1️⃣ 继承 video.js 组件系统

video.js 的组件化体系允许我们继承基础组件(如 Button),从而快速扩展功能。

复制代码
const Button = videojs.getComponent('Button');
class QualityDropdown extends Button { ... }
videojs.registerComponent('QualityDropdown', QualityDropdown);

这段代码的核心在于:

  • 保留原有按钮行为;

  • 增加自定义下拉菜单;

  • controlBar 中动态注册。

2️⃣ 动态生成菜单项

我们通过遍历 sources 数组,为每种清晰度创建一个 <div> 选项:
*

复制代码
  this.sources.forEach((source, index) => {
    const item = document.createElement('div');
    item.innerText = source.label;
    ...
  });

切换清晰度的核心逻辑

点击选项后,调用:
*

复制代码
  this.player().src(source);
  this.player().play();

重新设置视频源,并立即播放。

由于 video.js 会自动重新加载元数据,这种切换方式简单直接,兼容性很好。

相关推荐
前端不太难1 分钟前
RN 项目安全如何强化?(逆向、API安全、JS泄露)
开发语言·javascript·安全
Nan_Shu_6141 分钟前
学习:Pinia(1)
javascript·vue.js·学习
小白学大数据1 分钟前
实时监控 1688 商品价格变化的爬虫系统实现
javascript·爬虫·python
哆啦A梦158810 分钟前
商城后台管理系统 04 登录-功能实现-数据持久化-vuex
javascript·vue.js·elementui
音视频牛哥13 分钟前
C#实战:如何开发设计毫秒级延迟、工业级稳定的Windows平台RTSP/RTMP播放器
人工智能·机器学习·机器人·c#·音视频·rtsp播放器·rtmp播放器
Blossom.1181 小时前
基于时序大模型+强化学习的虚拟电厂储能调度系统:从负荷预测到收益最大化的实战闭环
运维·人工智能·python·决策树·机器学习·自动化·音视频
毕设源码-朱学姐9 小时前
【开题答辩全过程】以 工厂能耗分析平台的设计与实现为例,包含答辩的问题和答案
java·vue.js
老前端的功夫10 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
前端 贾公子11 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
JS-s11 小时前
【无标题】
音视频