|
| 1 | +const fs = require('fs'); |
| 2 | + |
| 3 | +// Configuration - edit these values as needed |
| 4 | +const rootStoragePath = '/storage/sd'; |
| 5 | +const assetsFolder = 'assets'; |
| 6 | + |
| 7 | +// State tracking |
| 8 | +let currentVideoIndex = 0; |
| 9 | +let visibleVideoPlayer = 1; |
| 10 | +let videoFiles = []; |
| 11 | + |
| 12 | +// Helper function to get player element by number |
| 13 | +function getPlayer(playerNumber) { |
| 14 | + return document.getElementById(`video-player-${playerNumber}`); |
| 15 | +} |
| 16 | + |
| 17 | +// Helper function to get the currently visible and hidden players |
| 18 | +function getPlayers() { |
| 19 | + const visible = getPlayer(visibleVideoPlayer); |
| 20 | + const hidden = getPlayer(visibleVideoPlayer === 1 ? 2 : 1); |
| 21 | + return { visible, hidden }; |
| 22 | +} |
| 23 | + |
| 24 | +async function main() { |
| 25 | + console.log('main() - App ready!'); |
| 26 | + |
| 27 | + try { |
| 28 | + // Load and filter video files |
| 29 | + videoFiles = fs.readdirSync(`${rootStoragePath}/${assetsFolder}`); |
| 30 | + videoFiles = videoFiles.filter(filename => !filename.startsWith('.')); |
| 31 | + videoFiles.sort(); |
| 32 | + |
| 33 | + // console.log('Video files:', videoFiles); |
| 34 | + |
| 35 | + if (videoFiles.length === 0) { |
| 36 | + console.error(`No video files found in ${rootStoragePath}/${assetsFolder}`); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const player1 = getPlayer(1); |
| 41 | + const player2 = getPlayer(2); |
| 42 | + |
| 43 | + // Initialize first video |
| 44 | + player1.src = `${assetsFolder}/${videoFiles[0]}`; |
| 45 | + player1.currentTime = 0; |
| 46 | + player1.muted = false; // Ensure audio is enabled for first player |
| 47 | + player2.muted = true; // Mute the second player initially |
| 48 | + console.log('Player 1 loaded:', videoFiles[0]); |
| 49 | + |
| 50 | + // Set up video ended listeners for both players |
| 51 | + player1.addEventListener('ended', () => switchToNextVideo()); |
| 52 | + player2.addEventListener('ended', () => switchToNextVideo()); |
| 53 | + |
| 54 | + // Preload next video once first video starts playing |
| 55 | + player1.addEventListener('playing', () => { |
| 56 | + console.log('Player 1 playing'); |
| 57 | + preloadNextVideo(); |
| 58 | + }, { once: true }); |
| 59 | + |
| 60 | + } catch (e) { |
| 61 | + console.error('Error in main():', e); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Preload the next video in the hidden player |
| 66 | +function preloadNextVideo() { |
| 67 | + const nextVideoIndex = (currentVideoIndex + 1) % videoFiles.length; |
| 68 | + const { hidden } = getPlayers(); |
| 69 | + const filePath = `${assetsFolder}/${videoFiles[nextVideoIndex]}`; |
| 70 | + |
| 71 | + console.log(`Preloading: ${videoFiles[nextVideoIndex]}`); |
| 72 | + |
| 73 | + // Reset and load the next video |
| 74 | + hidden.pause(); |
| 75 | + hidden.currentTime = 0; |
| 76 | + hidden.muted = true; // Ensure hidden player is muted |
| 77 | + hidden.src = filePath; |
| 78 | + hidden.load(); |
| 79 | +} |
| 80 | + |
| 81 | +// Switch to the next video seamlessly |
| 82 | +function switchToNextVideo() { |
| 83 | + currentVideoIndex = (currentVideoIndex + 1) % videoFiles.length; |
| 84 | + const { visible: currentPlayer, hidden: nextPlayer } = getPlayers(); |
| 85 | + |
| 86 | + console.log(`Switching to: ${videoFiles[currentVideoIndex]}`); |
| 87 | + |
| 88 | + // Ensure next player starts from beginning |
| 89 | + nextPlayer.currentTime = 0; |
| 90 | + |
| 91 | + // Start playing the next video (while still hidden) |
| 92 | + nextPlayer.play().catch(e => console.error('Play error:', e)); |
| 93 | + |
| 94 | + // Switch visibility and audio immediately - the video is already preloaded |
| 95 | + currentPlayer.pause(); |
| 96 | + currentPlayer.muted = true; // Mute the outgoing player |
| 97 | + currentPlayer.classList.add('hidden'); |
| 98 | + |
| 99 | + nextPlayer.muted = false; // Unmute the incoming player |
| 100 | + nextPlayer.classList.remove('hidden'); |
| 101 | + |
| 102 | + // Toggle which player is visible |
| 103 | + visibleVideoPlayer = visibleVideoPlayer === 1 ? 2 : 1; |
| 104 | + |
| 105 | + // Preload the next video in the now-hidden player |
| 106 | + preloadNextVideo(); |
| 107 | +} |
| 108 | + |
| 109 | +// Call main when DOM is ready |
| 110 | +if (document.readyState === 'loading') { |
| 111 | + document.addEventListener('DOMContentLoaded', main); |
| 112 | +} else { |
| 113 | + main(); |
| 114 | +} |
0 commit comments