当前位置: 首页 > ai >正文

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

步骤 1: 安装 Video.js

npm install video.js --save

步骤 2: 创建 VideoPlayer 组件

<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视频URLtype: '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 组件
<template><div class="play"><el-breadcrumbseparator-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播放器,以避免内存泄漏问题。
 

http://www.xdnf.cn/news/6618.html

相关文章:

  • EWOMAIL
  • Go语言实现生产者-消费者问题的多种方法
  • 【C++重载操作符与转换】句柄类与继承
  • 自定义CString类与MFC CString类接口对比
  • eSwitch manager 简介
  • InfluxDB 2.7 连续查询实战指南:Task 替代方案详解
  • python中元组的操作
  • 后端框架(2):Java的反射机制
  • 高效便捷的文字识别方案与解析
  • MATLAB中的概率分布生成:从理论到实践
  • 记录一次服务器卡顿
  • Redisson分布式锁-锁的可重入、可重试、WatchDog超时续约、multLock联锁(一文全讲透,超详细!!!)
  • SD框架下 LoRA 训练教程3-LORA学习率调度器(Learning Rate Scheduler)核心策略与实践指南
  • C++_STL_map与set
  • Java【13_1】final、初始化块、继承(测试题)
  • 每日Prompt:迷你 3D 建筑
  • pcie phy-电气层-gen1/2(TX)
  • C++ 条件变量与线程通知机制:std::condition_variable
  • PD 分离推理的加速大招,百度智能云网络基础设施和通信组件的优化实践
  • 【data】上海膜拜数据
  • AWS云入门宝典
  • STM32外设AD/DA-基础及CubeMX配置
  • Web性能优化的未来:边缘计算、AI与新型渲染架构
  • 排序01:多目标模型
  • SQL Server权限设置的几种方法
  • 每周靶点:CA125、AFP分享
  • Hue面试内容整理-示例编码题
  • 如何选择高性价比的 1T 服务器租用服务​
  • 【Android构建系统】了解Soong构建系统
  • JS手写代码篇---手写 instanceof 方法