Vue2项目中使用videojs播放mp4视频

步骤 1: 安装 Video.js

bash 复制代码
npm install video.js --save

步骤 2: 创建 VideoPlayer 组件

html 复制代码
<template>
  <div>
    <!-- Video.js player container -->
    <video ref="videoPlayer" class="video-js"></video>
  </div>
</template>

<script>
import videojs from 'video.js';
import 'video.js/dist/video-js.css'; // 引入 Video.js 默认样式

export default {
  name: 'VideoPlayer',
  props: {
    options: {
      type: Object,
      default() {
        return {};
      }
    },
    mp4src: {
      type: String,
      required: true,
    }
  },
  data() {
    return {
      player: null
    };
  },
  methods: {
    playerMedia() {
      this.player = videojs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
        console.log('Your player is ready!');
      });
      this.player.src({
        src: this.mp4src, // 替换为你的MP4视频URL
        type: 'video/mp4'
      });
    }
  },
  beforeDestroy() {
    if (this.player) {
      this.player.dispose(); // 清理资源
    }
  },
  watch: {
    mp4src: {
      handler: function(val) {
        val && this.playerMedia()
      },
      immediate: true,
    }
  }
};
</script>

<style scoped>
/* 自定义样式 */
.video-js {
  width: 100%;
  height: 100%;
  padding: 0 !important;
}
</style>
步骤 3: 在父组件中使用 VideoPlayer 组件
html 复制代码
<template>
  <div class="play">
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="padding: 10px 0px"
    >
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>详情</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="playDetail">
      <div class="playback">
        <p class="playback-title">
          {{ videoDetail['title'] }}
        </p>
      </div>
      <p class="introduction">
        简介:{{videoDetail['content']}}
      </p>
      <div class="play-video">
        <VideoPlayer style="width: 100%; height: 460px;" :options="playerOptions" :mp4src="videoDetail['src']" />
      </div>
    </div>
  </div>
</template>

<script>
import count from "@/api/home";
import VideoPlayer from "@/components/Lives/VideoPlayer.vue";
export default {
  components: {
    VideoPlayer
  },
  data(){
    return{
      playerOptions: {
        autoplay: true, // 是否自动播放
        controls: true, // 显示控制条
        sources: [],
        // poster: '/path/to/default.jpg' // 可选: 视频封面图片
      },
      videoDetail: {
        id: undefined,
        title: '',
        content: '',
        src: '',
      },
    }
  },
  created() {
    let id=this.$route.query.id;
    count.getVideoById({id}).then(res=>{
      console.log(res, '视频获取');
      const {code, msg, data} = res;
      if(code === 0 && msg === 'success') {
        for (const videoDetailKey in this.videoDetail) {
          if (data.hasOwnProperty(videoDetailKey)) {
            this.videoDetail[videoDetailKey] = data[videoDetailKey];
          }
        }
      }
    })
  },
};
</script>

<style lang="scss" scoped>
.play {
  width: 100%;
  height: 100%;
  padding: 64px 130px;
  background: #eee;
}
.playDetail {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 20px;
}
.playback {
  border-bottom: 1px solid #eee;
}
.playback-title {
  font-size: 24px;
  padding: 10px 0px;
  margin: 0;
  font-weight: bold;
}
.introduction {
  font-size: 14px;
  padding: 10px 0px;
  margin: 0;
  font-weight: bold;
}
.play-video{
    width: 100%;
    height: 460px;
}
</style>

在上面的示例中,首先引入videojs和video.js的CSS文件。然后创建了一个video标签,并使用ref属性来引用它。

在Vue组件的mounted钩子中,使用videojs初始化了播放器。将video标签传递给videojs构造函数,并在构造函数中传递了一些选项。然后,通过src方法将本地.mp4文件加载到播放器中,并使用play方法自动播放。

在组件销毁之前,使用了beforeDestroy钩子来销毁videojs播放器,以避免内存泄漏问题。

相关推荐
小徐_23333 分钟前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
winfredzhang8 分钟前
用 wxPython + ECharts + 阿里矢量地图,做一个可离线兜底的上海雨量看板
前端·javascript·echarts
深念Y13 分钟前
开发者如何清理和迁移C盘堆积的垃圾
c语言·开发语言
索西引擎20 分钟前
【React】Immer.js 在现代 Redux 生态中的角色:不可变性保障的工程化实现与开发体验优化
前端·javascript·react.js
kisshyshy22 分钟前
《川剧变脸 × React 状态管理?我在浏览器里跑了个端侧大模型》
前端·react.js·架构
只一23 分钟前
端侧AI实战第二章:React组件工程化 + 事件系统 + 可复用进度条(WebGPU模型加载底座)
前端·react.js
2501_9095091029 分钟前
DAY 28
开发语言·python
只一30 分钟前
拿捏大模型输出随机性:Temperature、Top-K 原理 + LangChain 工程落地实战
javascript·langchain
大卫陈32 分钟前
微信小程序虚拟支付实战:从「支付能力被限制」到沙箱调通的全过程
前端·后端
武子康32 分钟前
vLLM 0.25.1:服务没有报错,为什么仍会生成垃圾 Token(5 级正确性门禁 + 自动回滚条件)
前端·人工智能·后端