Skip to content

一分钟搭建影视站,支持Docker等部署方式

Add autoplay feature and improve progress bar interaction in player

developer6875a34c16ce
diff --git a/js/config.js b/js/config.js
index 6deaaaa..296b81f 100644
--- a/js/config.js
+++ b/js/config.js
@@ -82,7 +82,8 @@ const PLAYER_CONFIG = {
width: '100%',
height: '600',
timeout: 15000, // 播放器加载超时时间
- filterAds: true // 是否启用广告过滤
+ filterAds: true, // 是否启用广告过滤
+ autoPlayNext: true // 默认启用自动连播功能
};
// 增加错误信息本地化
diff --git a/player.html b/player.html
index d2af762..e9ddfcf 100644
--- a/player.html
+++ b/player.html
@@ -79,6 +79,45 @@
overflow-y: auto;
padding: 1rem 0;
}
+ .switch {
+ position: relative;
+ display: inline-block;
+ width: 46px;
+ height: 24px;
+ }
+ .switch input {
+ opacity: 0;
+ width: 0;
+ height: 0;
+ }
+ .slider {
+ position: absolute;
+ cursor: pointer;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ background-color: #333;
+ transition: .4s;
+ border-radius: 24px;
+ }
+ .slider:before {
+ position: absolute;
+ content: "";
+ height: 18px;
+ width: 18px;
+ left: 3px;
+ bottom: 3px;
+ background-color: white;
+ transition: .4s;
+ border-radius: 50%;
+ }
+ input:checked + .slider {
+ background-color: #00ccff;
+ }
+ input:checked + .slider:before {
+ transform: translateX(22px);
+ }
</style>
</head>
<body>
@@ -133,10 +172,15 @@
</div>
</div>
- <!-- 排序和选集 -->
+ <!-- 添加自动播放开关和排序按钮 -->
<div class="player-container">
- <div class="mb-4 flex justify-end">
- <button onclick="toggleEpisodeOrder()" class="px-4 py-2 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 text-white font-semibold rounded-full shadow-lg hover:shadow-xl transition-all duration-300 flex items-center justify-center space-x-2">
+ <div class="flex justify-end items-center mb-4 gap-2">
+ <span class="text-gray-400 text-sm">自动连播</span>
+ <label class="switch">
+ <input type="checkbox" id="autoplayToggle">
+ <span class="slider"></span>
+ </label>
+ <button onclick="toggleEpisodeOrder()" class="ml-4 px-4 py-2 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 text-white font-semibold rounded-full shadow-lg hover:shadow-xl transition-all duration-300 flex items-center justify-center space-x-2">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" id="orderIcon" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-11a1 1 0 10-2 0v3.586L7.707 9.293a1 1 0 00-1.414 1.414l3 3a1 1 0 001.414 0l3-3a1 1 0 00-1.414-1.414L11 10.586V7z" clip-rule="evenodd" />
</svg>
@@ -167,6 +211,10 @@
let episodesReversed = false;
let dp = null;
let currentHls = null; // 跟踪当前HLS实例
+ let autoplayEnabled = true; // 默认开启自动连播
+ let isUserSeeking = false; // 跟踪用户是否正在拖动进度条
+ let videoHasEnded = false; // 跟踪视频是否已经自然结束
+ let userClickedPosition = null; // 记录用户点击的位置
// 页面加载
document.addEventListener('DOMContentLoaded', function() {
@@ -180,6 +228,16 @@
currentVideoTitle = title || localStorage.getItem('currentVideoTitle') || '未知视频';
currentEpisodeIndex = index;
+ // 设置自动连播开关状态
+ autoplayEnabled = localStorage.getItem('autoplayEnabled') !== 'false'; // 默认为true
+ document.getElementById('autoplayToggle').checked = autoplayEnabled;
+
+ // 监听自动连播开关变化
+ document.getElementById('autoplayToggle').addEventListener('change', function(e) {
+ autoplayEnabled = e.target.checked;
+ localStorage.setItem('autoplayEnabled', autoplayEnabled);
+ });
+
try {
currentEpisodes = JSON.parse(localStorage.getItem('currentEpisodes') || '[]');
episodesReversed = localStorage.getItem('episodesReversed') === 'true';
@@ -211,6 +269,11 @@
// 更新排序按钮状态
updateOrderButton();
+
+ // 添加对进度条的监听,确保点击准确跳转
+ setTimeout(() => {
+ setupProgressBarPreciseClicks();
+ }, 1000);
});
// 初始化播放器
@@ -373,6 +436,10 @@
dp.on('loadedmetadata', function() {
document.getElementById('loading').style.display = 'none';
+ videoHasEnded = false; // 视频加载时重置结束标志
+
+ // 视频加载完成后重新设置进度条点击监听
+ setupProgressBarPreciseClicks();
});
dp.on('error', function() {
@@ -384,6 +451,78 @@
showError('视频播放失败,请检查视频源或网络连接');
});
+ // 添加seeking和seeked事件监听器,以检测用户是否在拖动进度条
+ dp.on('seeking', function() {
+ isUserSeeking = true;
+ videoHasEnded = false; // 重置视频结束标志
+
+ // 如果是用户通过点击进度条设置的位置,确保准确跳转
+ if (userClickedPosition !== null && dp.video) {
+ // 确保用户的点击位置被正确应用,避免自动跳至视频末尾
+ const clickedTime = userClickedPosition;
+
+ // 防止跳转到视频结尾
+ if (Math.abs(dp.video.duration - clickedTime) < 0.5) {
+ // 如果点击的位置非常接近结尾,稍微减少一点时间
+ dp.video.currentTime = Math.max(0, clickedTime - 0.5);
+ } else {
+ dp.video.currentTime = clickedTime;
+ }
+
+ // 清除记录的位置
+ setTimeout(() => {
+ userClickedPosition = null;
+ }, 200);
+ }
+ });
+
+ // 改进seeked事件处理
+ dp.on('seeked', function() {
+ // 如果视频跳转到了非常接近结尾的位置(小于0.3秒),且不是自然播放到此处
+ if (dp.video && dp.video.duration > 0) {
+ const timeFromEnd = dp.video.duration - dp.video.currentTime;
+ if (timeFromEnd < 0.3 && isUserSeeking) {
+ // 将播放时间往回移动一点点,避免触发结束事件
+ dp.video.currentTime = Math.max(0, dp.video.currentTime - 1);
+ }
+ }
+
+ // 延迟重置seeking标志,以便于区分自然播放结束和用户拖拽
+ setTimeout(() => {
+ isUserSeeking = false;
+ }, 200);
+ });
+
+ // 修改视频结束事件监听器,添加额外检查
+ dp.on('ended', function() {
+ videoHasEnded = true; // 标记视频已自然结束
+
+ // 如果启用了自动连播,并且有下一集可播放,则自动播放下一集
+ if (autoplayEnabled && currentEpisodeIndex < currentEpisodes.length - 1) {
+ console.log('视频播放结束,自动播放下一集');
+ // 稍长延迟以确保所有事件处理完成
+ setTimeout(() => {
+ // 确认不是因为用户拖拽导致的假结束事件
+ if (videoHasEnded && !isUserSeeking) {
+ playNextEpisode();
+ videoHasEnded = false; // 重置标志
+ }
+ }, 1000);
+ } else {
+ console.log('视频播放结束,无下一集或未启用自动连播');
+ }
+ });
+
+ // 添加事件监听以检测近视频末尾的点击拖动
+ dp.on('timeupdate', function() {
+ if (dp.video && dp.duration > 0) {
+ // 如果视频接近结尾但不是自然播放到结尾,重置自然结束标志
+ if (isUserSeeking && dp.video.currentTime > dp.video.duration * 0.95) {
+ videoHasEnded = false;
+ }
+ }
+ });
+
// 10秒后如果仍在加载,但不立即显示错误
setTimeout(function() {
// 如果视频已经播放开始,则不显示错误
@@ -540,6 +679,7 @@
const url = currentEpisodes[index];
currentEpisodeIndex = index;
+ videoHasEnded = false; // 重置视频结束标志
// 更新URL,不刷新页面
const newUrl = new URL(window.location.href);
@@ -577,6 +717,9 @@
updateEpisodeInfo();
updateButtonStates();
renderEpisodes();
+
+ // 重置用户点击位置记录
+ userClickedPosition = null;
}
// 播放上一集
@@ -617,6 +760,82 @@
orderIcon.style.transform = episodesReversed ? 'rotate(180deg)' : '';
}
}
+
+ // 设置进度条准确点击处理
+ function setupProgressBarPreciseClicks() {
+ // 查找DPlayer的进度条元素
+ const progressBar = document.querySelector('.dplayer-bar-wrap');
+ if (!progressBar || !dp || !dp.video) return;
+
+ // 移除可能存在的旧事件监听器
+ progressBar.removeEventListener('mousedown', handleProgressBarClick);
+
+ // 添加新的事件监听器
+ progressBar.addEventListener('mousedown', handleProgressBarClick);
+
+ // 在移动端也添加触摸事件支持
+ progressBar.removeEventListener('touchstart', handleProgressBarTouch);
+ progressBar.addEventListener('touchstart', handleProgressBarTouch);
+
+ console.log('进度条精确点击监听器已设置');
+ }
+
+ // 处理进度条点击
+ function handleProgressBarClick(e) {
+ if (!dp || !dp.video) return;
+
+ // 计算点击位置相对于进度条的比例
+ const rect = e.currentTarget.getBoundingClientRect();
+ const percentage = (e.clientX - rect.left) / rect.width;
+
+ // 计算点击位置对应的视频时间
+ const duration = dp.video.duration;
+ let clickTime = percentage * duration;
+
+ // 处理视频接近结尾的情况
+ if (duration - clickTime < 1) {
+ // 如果点击位置非常接近结尾,稍微往前移一点
+ clickTime = Math.min(clickTime, duration - 1.5);
+ console.log(`进度条点击接近结尾,调整时间为 ${clickTime.toFixed(2)}/${duration.toFixed(2)}`);
+ }
+
+ // 记录用户点击的位置
+ userClickedPosition = clickTime;
+
+ // 输出调试信息
+ console.log(`进度条点击: ${percentage.toFixed(4)}, 时间: ${clickTime.toFixed(2)}/${duration.toFixed(2)}`);
+
+ // 阻止默认事件传播,避免DPlayer内部逻辑将视频跳至末尾
+ e.stopPropagation();
+
+ // 直接设置视频时间
+ dp.seek(clickTime);
+ }
+
+ // 处理移动端触摸事件
+ function handleProgressBarTouch(e) {
+ if (!dp || !dp.video || !e.touches[0]) return;
+
+ const touch = e.touches[0];
+ const rect = e.currentTarget.getBoundingClientRect();
+ const percentage = (touch.clientX - rect.left) / rect.width;
+
+ const duration = dp.video.duration;
+ let clickTime = percentage * duration;
+
+ // 处理视频接近结尾的情况
+ if (duration - clickTime < 1) {
+ clickTime = Math.min(clickTime, duration - 1.5);
+ }
+
+ // 记录用户点击的位置
+ userClickedPosition = clickTime;
+
+ console.log(`进度条触摸: ${percentage.toFixed(4)}, 时间: ${clickTime.toFixed(2)}/${duration.toFixed(2)}`);
+
+ e.stopPropagation();
+ dp.seek(clickTime);
+ }
</script>
</body>
</html>
diff --git a/readme.md b/readme.md
index 2ca4c1e..ff834e0 100644
--- a/readme.md
+++ b/readme.md
@@ -138,9 +138,9 @@ LibreTV/
└── sitemap.xml // 站点地图
```
-## ⭐ Star 历史
+## Star History
-[![Star History Chart](https://api.star-history.com/svg?repos=bestZwei/LibreTV&type=Date)](https://star-history.com/#bestZwei/LibreTV&Date)
+[![Star History Chart](https://api.star-history.com/svg?repos=bestZwei/LibreTV&type=Date)](https://www.star-history.com/#bestZwei/LibreTV&Date)
## ⚠️ 免责声明