1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 01:39:24 +02:00

Fix playback start

This commit is contained in:
Daniel Neto 2025-01-15 15:43:06 -03:00
parent ff20a1b754
commit 5e6e50f9ad
10 changed files with 111 additions and 95 deletions

View file

@ -669,7 +669,7 @@ class Category
global $global, $config; global $global, $config;
$sql = "SELECT count(id) as total FROM categories WHERE parentId=? AND id!=? "; $sql = "SELECT count(id) as total FROM categories WHERE parentId=? AND id!=? ";
$res = sqlDAL::readSql($sql, "i", [$parentId]); $res = sqlDAL::readSql($sql, "ii", [$parentId, $parentId]);
$result = sqlDAL::fetchAssoc($res); $result = sqlDAL::fetchAssoc($res);
sqlDAL::close($res); sqlDAL::close($res);
if ($res) { if ($res) {

View file

@ -335,7 +335,7 @@ function linkifyTimestamps($text) {
} }
// Return the clickable link // Return the clickable link
return "<a href='javascript:void(0)' onclick=\"player.currentTime($seconds);\">$timestamp</a>"; return "<a href='javascript:void(0)' onclick=\"console.log('objects-functionsSecurity.php player.currentTime');player.currentTime($seconds);\">$timestamp</a>";
}; };
// Replace timestamps with links // Replace timestamps with links

View file

@ -231,6 +231,7 @@
if (!preventDefault) { if (!preventDefault) {
var key = this.getAttribute('data-marker-key'); var key = this.getAttribute('data-marker-key');
console.log('currentTime video markers');
player.currentTime(setting.markerTip.time(markersMap[key])); player.currentTime(setting.markerTip.time(markersMap[key]));
} }
}); });
@ -459,6 +460,7 @@
for (var i = 0; i < markersList.length; i++) { for (var i = 0; i < markersList.length; i++) {
var markerTime = setting.markerTip.time(markersList[i]); var markerTime = setting.markerTip.time(markersList[i]);
if (markerTime > currentTime) { if (markerTime > currentTime) {
console.log('currentTime video markers 2');
player.currentTime(markerTime); player.currentTime(markerTime);
break; break;
} }
@ -471,6 +473,7 @@
var markerTime = setting.markerTip.time(markersList[i]); var markerTime = setting.markerTip.time(markersList[i]);
// add a threshold // add a threshold
if (markerTime + 0.5 < currentTime) { if (markerTime + 0.5 < currentTime) {
console.log('currentTime video markers 3');
player.currentTime(markerTime); player.currentTime(markerTime);
return; return;
} }

View file

@ -30,9 +30,11 @@ this.el().addEventListener('touchend', function(event) {
var touchX = event.changedTouches[0].clientX; var touchX = event.changedTouches[0].clientX;
var videoWidth = player.el().clientWidth; var videoWidth = player.el().clientWidth;
if (touchX < videoWidth / 2) { if (touchX < videoWidth / 2) {
console.log('currentTime hot key 1');
// Double tap on the left half - rewind // Double tap on the left half - rewind
player.currentTime(player.currentTime() - 10); player.currentTime(player.currentTime() - 10);
} else { } else {
console.log('currentTime hot key 2');
// Double tap on the right half - forward // Double tap on the right half - forward
player.currentTime(player.currentTime() + 10); player.currentTime(player.currentTime() + 10);
} }

View file

@ -11,9 +11,11 @@ window.addEventListener('message', function(event) {
} }
break; break;
case 'rewind': case 'rewind':
console.log('currentTime hot key rewind');
player.currentTime(player.currentTime() - 10); player.currentTime(player.currentTime() - 10);
break; break;
case 'forward': case 'forward':
console.log('currentTime hot key forward');
player.currentTime(player.currentTime() + 10); player.currentTime(player.currentTime() + 10);
break; break;
case 'volumeUp': case 'volumeUp':

View file

@ -203,6 +203,7 @@ echo AVideoPlugin::afterVideoJS();
return false; return false;
}); });
console.log('currentTime player 1');
player.currentTime(playerPlaylist[0].videoStartSeconds); player.currentTime(playerPlaylist[0].videoStartSeconds);
$("#modeYoutubeBottomContent").load("<?php echo $global['webSiteRootURL']; ?>view/modeYoutubeBottom.php?videos_id=" + playerPlaylist[0].videos_id); $("#modeYoutubeBottomContent").load("<?php echo $global['webSiteRootURL']; ?>view/modeYoutubeBottom.php?videos_id=" + playerPlaylist[0].videos_id);
$(".vjs-playlist-item ").click(function() { $(".vjs-playlist-item ").click(function() {
@ -215,6 +216,7 @@ echo AVideoPlugin::afterVideoJS();
$("#modeYoutubeBottomContent").load("<?php echo $global['webSiteRootURL']; ?>view/modeYoutubeBottom.php?videos_id=" + playerPlaylist[index].videos_id); $("#modeYoutubeBottomContent").load("<?php echo $global['webSiteRootURL']; ?>view/modeYoutubeBottom.php?videos_id=" + playerPlaylist[index].videos_id);
if (playerPlaylist[index] && playerPlaylist[index].videoStartSeconds) { if (playerPlaylist[index] && playerPlaylist[index].videoStartSeconds) {
setTimeout(function() { setTimeout(function() {
console.log('currentTime player 2');
player.currentTime(playerPlaylist[index].videoStartSeconds); player.currentTime(playerPlaylist[index].videoStartSeconds);
}, 500); }, 500);
} }

View file

@ -1,6 +1,12 @@
// Queue to track skipped ads // Queue to track skipped ads
let skippedAdsQueue = []; let skippedAdsQueue = [];
// Array to store scheduled ad times
let scheduledAdTimes = [];
// Dynamic interval for live ads
let liveAdInterval = null;
// Fetch and parse VMAP // Fetch and parse VMAP
if (typeof _adTagUrl === 'string') { if (typeof _adTagUrl === 'string') {
fetch(_adTagUrl) fetch(_adTagUrl)
@ -14,12 +20,12 @@ if (typeof _adTagUrl === 'string') {
console.log('timeOffset found', timeOffset); console.log('timeOffset found', timeOffset);
if (timeOffset === 'start') { if (timeOffset === 'start') {
scheduleAd(0); scheduledAdTimes.push(0);
} else if (timeOffset === 'end') { } else if (timeOffset === 'end') {
scheduleAdAtEnd(); scheduledAdTimes.push('end');
} else if (timeOffset) { } else if (timeOffset) {
var seconds = convertTimeOffsetToSeconds(timeOffset); var seconds = convertTimeOffsetToSeconds(timeOffset);
scheduleAd(seconds); scheduledAdTimes.push(seconds);
} }
}); });
}) })
@ -39,95 +45,95 @@ function convertTimeOffsetToSeconds(timeOffset) {
// Ensure ad display container is initialized on mobile // Ensure ad display container is initialized on mobile
function initializeAdContainer() { function initializeAdContainer() {
if (player.ima && typeof player.ima.initializeAdDisplayContainer === "function") { if (player.ima && typeof player.ima.initializeAdDisplayContainer === "function") {
console.log(`scheduleAd: initializeAdDisplayContainer`); console.log(`initializeAdContainer: Initializing ad container`);
player.ima.initializeAdDisplayContainer(); player.ima.initializeAdDisplayContainer();
} }
} }
// Schedule ad playback // Function to dynamically schedule ads
function scheduleAd(seconds) { function scheduleAd(seconds) {
if (seconds > 5) { if (player.liveTracker && player.liveTracker.isLive()) {
for (let index = 5; index > 0; index--) { liveAdInterval = seconds; // Set live ad interval dynamically
setTimeout(() => { console.log(`Live ad interval set to ${seconds} seconds`);
console.log(`scheduleAd: Ad will play in ${index} seconds at ${seconds} seconds`); setupLiveAdInterval();
}, (seconds - index) * 1000); } else {
scheduledAdTimes.push(seconds);
} }
} }
setTimeout(() => {
console.log(`scheduleAd: Checking playback state for ad at ${seconds} seconds`);
if (!player.paused()) {
console.log(`scheduleAd: Triggering ad at ${seconds} seconds`);
try {
// Ensure the ad container is ready (for mobile)
initializeAdContainer();
// Request and play ads // Function to set up live ad interval
function setupLiveAdInterval() {
if (liveAdInterval && player.liveTracker && player.liveTracker.isLive()) {
setInterval(() => {
console.log(`Triggering live ad every ${liveAdInterval} seconds`);
try {
initializeAdContainer();
player.ima.requestAds(); player.ima.requestAds();
} catch (error) { } catch (error) {
console.error(`scheduleAd: Error while triggering ad: ${error.message}`); console.error(`Error triggering live ad: ${error.message}`);
} }
} else { }, liveAdInterval * 1000);
if(checkIfAdIsPlaying()){
console.log(`scheduleAd: ad is already playing, skipping ad at ${seconds} seconds`);
}else{
skippedAdsQueue.push(() => {
console.log('Playing skipped ad at the end of the video');
initializeAdContainer();
player.ima.requestAds();
});
console.log(`scheduleAd: Video is paused, skipping ad at ${seconds} seconds`);
}
// Add logic to handle skipped ads if needed
}
}, seconds * 1000);
}
// Schedule ad at the end of the video
function scheduleAdAtEnd() {
player.on('timeupdate', function handleTimeUpdate() {
const remainingTime = player.duration() - player.currentTime();
// Countdown messages for the ad
if (remainingTime <= 10 && remainingTime > 5) {
const countdown = Math.floor(remainingTime - 5);
for (let index = countdown; index > 0; index--) {
setTimeout(() => {
console.log(`scheduleAdAtEnd: Ad will play in ${index} seconds at the end of the video`);
}, (countdown - index) * 1000);
} }
} }
// Check if the ad should be triggered // Function to check and play ads based on current time
if (remainingTime <= 5) { function checkAndPlayAds() {
console.log('scheduleAdAtEnd: Checking playback state for ad at the end of the video'); const currentTime = player.currentTime();
scheduledAdTimes = scheduledAdTimes.filter(adTime => {
if (adTime === 'end') return true; // Keep the "end" marker for later handling
if (currentTime >= adTime) {
console.log(`Triggering ad scheduled for ${adTime} seconds`);
try { try {
if (!player.paused()) {
console.log('scheduleAdAtEnd: Triggering ad at the end of the video');
initializeAdContainer(); initializeAdContainer();
player.ima.requestAds(); player.ima.requestAds();
player.off('timeupdate', handleTimeUpdate); // Remove listener after ad is triggered
} else {
if (checkIfAdIsPlaying()) {
console.log('scheduleAdAtEnd: Ad is already playing, skipping ad at the end of the video');
} else {
skippedAdsQueue.push(() => {
console.log('Playing skipped ad at the end of the video');
initializeAdContainer();
player.ima.requestAds();
});
console.log('scheduleAdAtEnd: Video is paused, skipping ad at the end of the video');
}
player.off('timeupdate', handleTimeUpdate); // Remove listener
}
} catch (error) { } catch (error) {
console.error(`scheduleAdAtEnd: Error while triggering ad: ${error.message}`); console.error(`Error triggering ad at ${adTime} seconds: ${error.message}`);
} }
return false; // Remove this ad time as it has been triggered
} }
return true;
}); });
} }
// Play skipped ads after the video resumes // Function to check if an ad is playing
function checkIfAdIsPlaying() {
console.log('Is an ad playing?', isAdPlaying);
return isAdPlaying;
}
function fixAdPlaying(){
if(isAdPlaying){
if (!player.paused()) {
console.log('fixAdPlaying: Pausing main video');
player.pause(); // Pause the main video
}
}else{
if (player.paused()) {
console.log('fixAdPlaying: Resuming main video');
player.play(); // Resume the main video
}
}
}
// Set up event listener to check ad triggers on time updates
player.on('timeupdate', () => {
checkAndPlayAds();
});
// Handle ads at the end of the video
player.on('ended', () => {
if (scheduledAdTimes.includes('end')) {
console.log('Playing ad at the end of the video');
try {
initializeAdContainer();
player.ima.requestAds();
} catch (error) {
console.error(`Error triggering ad at the end of the video: ${error.message}`);
}
}
});
// Play skipped ads when the video resumes
player.on('play', () => { player.on('play', () => {
while (skippedAdsQueue.length > 0) { while (skippedAdsQueue.length > 0) {
const playSkippedAd = skippedAdsQueue.shift(); const playSkippedAd = skippedAdsQueue.shift();
@ -135,34 +141,27 @@ player.on('play', () => {
} }
}); });
let isAdPlaying = false;
// Listen for ad start event
player.on('ads-ad-started', () => { player.on('ads-ad-started', () => {
isAdPlaying = true; isAdPlaying = true;
console.log('Ad started playing'); console.log('Ad started playing');
fixAdPlaying();
}); });
// Listen for ad end event // Listen for ad end event to resume the main video
player.on('ads-ad-ended', () => { player.on('ads-ad-ended', () => {
isAdPlaying = false; isAdPlaying = false;
console.log('Ad finished playing'); console.log('Ad finished playing');
fixAdPlaying();
}); });
// Listen for ad error // Listen for ad error and reset playback states if needed
player.on('adserror', () => { player.on('adserror', () => {
isAdPlaying = false; isAdPlaying = false;
console.log('Ad playback encountered an error'); console.log('Ad playback encountered an error, resuming main video if paused');
fixAdPlaying();
}); });
// Example usage // Example usage
player.on('ads-manager', function(response) { player.on('ads-manager', function(response) {
console.log('Ads manager ready:', response.adsManager); console.log('Ads manager ready:', response.adsManager);
}); });
// Function to check if an ad is playing
function checkIfAdIsPlaying() {
console.log('Is an ad playing?', isAdPlaying);
return isAdPlaying;
}

View file

@ -30,10 +30,12 @@ if (empty($MediaMetadata)) {
}); });
setActionHandlerIfSupported('seekbackward', function() { setActionHandlerIfSupported('seekbackward', function() {
/* Code excerpted. */ /* Code excerpted. */
console.log('currentTime mediasession 1');
player.currentTime(player.currentTime() - 5); player.currentTime(player.currentTime() - 5);
}); });
setActionHandlerIfSupported('seekforward', function() { setActionHandlerIfSupported('seekforward', function() {
/* Code excerpted. */ /* Code excerpted. */
console.log('currentTime mediasession 1');
player.currentTime(player.currentTime() + 5); player.currentTime(player.currentTime() + 5);
}); });
setActionHandlerIfSupported('seekto', function() { setActionHandlerIfSupported('seekto', function() {

View file

@ -22,6 +22,7 @@ function startTrackDisplay() {
$('#forwardLayer').remove(); $('#forwardLayer').remove();
}); });
}, 100); }, 100);
console.log('currentTime seek 1');
player.currentTime(player.currentTime() + playerSeekForward); player.currentTime(player.currentTime() + playerSeekForward);
} else if (e.offsetX < 0.33 * playerWidth) { } else if (e.offsetX < 0.33 * playerWidth) {
$(backLayer).prependTo("#mainVideo"); $(backLayer).prependTo("#mainVideo");
@ -31,6 +32,7 @@ function startTrackDisplay() {
$('#backLayer').remove(); $('#backLayer').remove();
}); });
}, 100); }, 100);
console.log('currentTime seek 2');
player.currentTime((player.currentTime() - playerSeekBack) < 0 ? 0 : (player.currentTime() - playerSeekBack)); player.currentTime((player.currentTime() - playerSeekBack) < 0 ? 0 : (player.currentTime() - playerSeekBack));
} else { } else {
if (player.paused()) { if (player.paused()) {

View file

@ -296,6 +296,7 @@ async function lazyImage() {
} }
var pauseIfIsPlayinAdsInterval; var pauseIfIsPlayinAdsInterval;
async function setPlayerListners() { async function setPlayerListners() {
if (typeof player !== 'undefined') { if (typeof player !== 'undefined') {
player.on('pause', function () { player.on('pause', function () {
@ -307,12 +308,13 @@ async function setPlayerListners() {
player.on('play', function () { player.on('play', function () {
isTryingToPlay = false; isTryingToPlay = false;
cancelAllPlaybackTimeouts(); cancelAllPlaybackTimeouts();
/* this was messing um the start playback time
if (startCurrentTime) { if (startCurrentTime) {
setTimeout(function () { setTimeout(function () {
setCurrentTime(startCurrentTime); setCurrentTime(startCurrentTime);
startCurrentTime = 0; startCurrentTime = 0;
}, 100); }, 100);
} }*/
//console.log("setPlayerListners: play"); //console.log("setPlayerListners: play");
//userIsControling = true; //userIsControling = true;
pauseIfIsPlayinAdsInterval = setInterval(function () { pauseIfIsPlayinAdsInterval = setInterval(function () {
@ -1109,6 +1111,7 @@ function setCurrentTime(currentTime) {
return false; // if is trying to play, only update if the time is greater return false; // if is trying to play, only update if the time is greater
} }
} }
console.trace();
console.log("setCurrentTime 1: ", currentTime); console.log("setCurrentTime 1: ", currentTime);
player.currentTime(currentTime); player.currentTime(currentTime);
initdone = false; initdone = false;
@ -1121,6 +1124,7 @@ function setCurrentTime(currentTime) {
// events: https://www.w3.org/TR/html5/embedded-content-0.html#mediaevents // events: https://www.w3.org/TR/html5/embedded-content-0.html#mediaevents
player.on("canplaythrough", function () { player.on("canplaythrough", function () {
if (!initdone) { if (!initdone) {
console.trace();
console.log('setCurrentTime canplaythrough', currentTime); console.log('setCurrentTime canplaythrough', currentTime);
player.currentTime(currentTime); player.currentTime(currentTime);
initdone = true; initdone = true;