使用 HTML5 <video> 标签(最简单、最推荐)
这是现代网页的标准做法,无需任何外部库,直接使用浏览器原生功能,代码简洁,兼容性好。

(图片来源网络,侵删)
基础代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">基础 MP4 播放器</title>
<style>
/* 让视频播放器宽度为100%,高度自适应,并居中显示 */
video {
width: 100%;
max-width: 800px;
display: block;
margin: 50px auto;
}
</style>
</head>
<body>
<h1>我的视频播放器</h1>
<!--
<video> 标签是核心。
src: 指向你的 MP4 文件路径(可以是本地路径或网络 URL)。
controls: 浏览器会自动提供播放、暂停、音量、进度条等控件。
width/height: 可选,设置播放器尺寸,但更推荐用 CSS 控制。
-->
<video src="path/to/your/video.mp4" controls></video>
</body>
</html>
如何使用:
- 将上面的代码保存为一个
.html文件(player.html)。 - 将你的
video.mp4文件放在与player.html相同的目录下,或者修改src属性为你的视频文件的正确路径。 - 用浏览器打开
player.html文件即可。
使用 <video> 标签 + 自定义控件(进阶)
如果你不希望使用浏览器默认的控件,或者想设计自己的UI,你可以通过监听视频事件和调用 JavaScript API 来实现。
完整代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">自定义控件 MP4 播放器</title>
<style>
body {
font-family: sans-serif;
text-align: center;
background-color: #f0f0f0;
}
.video-container {
position: relative;
max-width: 800px;
margin: 20px auto;
background-color: #000;
}
video {
width: 100%;
display: block;
}
/* 自定义控件容器 */
.custom-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
color: white;
padding: 10px;
opacity: 1;
transition: opacity 0.3s;
}
/* 鼠标悬停时保持控件可见 */
.video-container:hover .custom-controls {
opacity: 1;
}
.controls-row {
display: flex;
align-items: center;
gap: 10px;
}
button {
background: none;
border: none;
color: white;
cursor: pointer;
font-size: 16px;
}
input[type="range"] {
flex-grow: 1;
cursor: pointer;
}
.time-display {
min-width: 100px;
font-size: 12px;
}
</style>
</head>
<body>
<h1>自定义控件播放器</h1>
<div class="video-container">
<video id="myVideo">
<source src="path/to/your/video.mp4" type="video/mp4">
<!-- 提供兼容性提示 -->
您的浏览器不支持 HTML5 视频。
</video>
<div class="custom-controls">
<div class="controls-row">
<button id="playPauseBtn">▶️</button>
<input type="range" id="seekBar" value="0">
<span class="time-display" id="timeDisplay">0:00 / 0:00</span>
<button id="muteBtn">🔊</button>
<input type="range" id="volumeBar" min="0" max="1" step="0.1" value="1">
</div>
</div>
</div>
<script>
const video = document.getElementById('myVideo');
const playPauseBtn = document.getElementById('playPauseBtn');
const seekBar = document.getElementById('seekBar');
const timeDisplay = document.getElementById('timeDisplay');
const muteBtn = document.getElementById('muteBtn');
const volumeBar = document.getElementById('volumeBar');
// 1. 播放/暂停
playPauseBtn.addEventListener('click', () => {
if (video.paused) {
video.play();
playPauseBtn.textContent = '⏸️';
} else {
video.pause();
playPauseBtn.textContent = '▶️';
}
});
// 2. 更新进度条
video.addEventListener('timeupdate', () => {
// 更新进度条
const percentage = (video.currentTime / video.duration) * 100;
seekBar.value = percentage;
// 更新时间显示
const currentTime = formatTime(video.currentTime);
const duration = formatTime(video.duration);
timeDisplay.textContent = `${currentTime} / ${duration}`;
});
// 3. 点击进度条跳转
seekBar.addEventListener('input', () => {
const time = (seekBar.value / 100) * video.duration;
video.currentTime = time;
});
// 4. 静音/取消静音
muteBtn.addEventListener('click', () => {
video.muted = !video.muted;
muteBtn.textContent = video.muted ? '🔇' : '🔊';
});
// 5. 调整音量
volumeBar.addEventListener('input', () => {
video.volume = volumeBar.value;
});
// 视频结束时重置播放按钮
video.addEventListener('ended', () => {
playPauseBtn.textContent = '▶️';
});
// 辅助函数:格式化时间 (秒 -> mm:ss)
function formatTime(seconds) {
if (isNaN(seconds)) return '0:00';
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
}
</script>
</body>
</html>
使用第三方播放器库(功能最强大)
对于专业项目,如视频网站、在线教育平台等,通常会选择成熟的第三方播放器库,它们提供了更丰富的功能、更好的兼容性、更美观的UI和更强大的插件系统。
这里以 Video.js 为例,它是目前最流行、最强大的开源 HTML5 视频播放器之一。
步骤 1: 引入 Video.js 的 CSS 和 JS 文件
你可以从 Video.js 官网 下载文件,或者直接使用 CDN(推荐)。
<!-- 引入 Video.js 的 CSS --> <link href="https://vjs.zencdn.net/8.6.1/video-js.css" rel="stylesheet"> <!-- 引入 Video.js 的 JS --> <script src="https://vjs.zencdn.net/8.6.1/video.min.js"></script>
步骤 2: 编写 HTML 和初始化播放器
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">Video.js 播放器</title>
<!-- 引入 Video.js 的 CSS -->
<link href="https://vjs.zencdn.net/8.6.1/video-js.css" rel="stylesheet">
<style>
body {
text-align: center;
padding-top: 40px;
}
/* 播放器容器,设置一个固定高度 */
.video-js {
width: 800px;
height: 450px;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>使用 Video.js 的播放器</h1>
<!--
Video.js 的播放器容器。
class="video-js vjs-default-skin" 是必需的类。
data-setup='{}' 用于初始化配置。
controls 属性显示控件。
注意:使用 <source> 标签来指定视频源。
-->
<video
id="my-video"
class="video-js vjs-default-skin"
controls
preload="auto"
width="800"
height="450"
data-setup='{}'>
<source src="path/to/your/video.mp4" type="video/mp4">
<p class="vjs-no-js">
要播放此视频,你需要启用 JavaScript,并安装支持 HTML5 video 的播放器。
<a href="https://videojs.com/html5-video-support/" target="_blank">
查看哪些浏览器支持
</a>
</p>
</video>
<!-- 引入 Video.js 的 JS -->
<script src="https://vjs.zencdn.net/8.6.1/video.min.js"></script>
<!-- 你可以在这里写自定义的 JS 代码来控制播放器 -->
<script>
// Video.js 会在页面加载完成后自动初始化
// 你也可以手动获取播放器实例
const player = videojs('my-video');
// 监听播放结束事件
player.on('ended', function() {
console.log('视频播放完毕!');
});
</script>
</body>
</html>
Video.js 的优势:
- 丰富的 UI 主题:可以轻松切换皮肤。
- 插件系统:支持广告、画中画、截图、字幕等多种插件。
- 响应式设计:在不同设备上都能良好显示。
- 强大的 API:提供了全面的 JavaScript API 用于控制播放器。
总结与选择建议
| 方案 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
原生 <video> |
简单、快速、零依赖,浏览器原生支持。 | UI 和功能由浏览器决定,无法自定义,功能有限。 | 个人博客、项目内嵌视频、快速原型、对UI要求不高的场景。 |
| 自定义控件 | UI 完全可控,功能可以按需增减,学习成本低。 | 需要自己编写大量 JS 代码处理事件和状态,功能实现复杂。 | 需要特定品牌风格、交互方式的网站,UI 设计要求高的项目。 |
| 第三方库 (Video.js) | 功能强大、UI 美观、插件丰富、兼容性好,有专业支持。 | 需要引入外部资源,有一定学习成本。 | 商业视频网站、在线教育、流媒体平台、对功能和专业性要求高的项目。 |
给你的建议:
- 如果只是想在网页上放个视频播放一下,直接用方案一。
- 如果你想做一个看起来很酷、符合你网站风格的播放器,并且愿意花时间写点 JS,尝试方案二。
- 如果你在做一个正经的视频产品,或者需要播放列表、广告、字幕等高级功能,直接用方案三,Video.js 是一个非常优秀的选择。
