ShowerLoop-cc/docker/showerloop/public/js/hls-player.min.js

2 lines
1.8 KiB
JavaScript

document.addEventListener("DOMContentLoaded",function(){const videos=document.querySelectorAll("video");if(0!==videos.length){function isHlsSupported(){const video=document.createElement("video");return video.canPlayType("application/vnd.apple.mpegURL")||video.canPlayType("application/x-mpegURL")}function setupHlsJs(video){const hlsSource=Array.from(video.querySelectorAll("source")).find(source=>source.src.includes(".m3u8"));if(hlsSource){if(!isHlsSupported()){const mp4Source=Array.from(video.querySelectorAll("source")).find(source=>"video/mp4"===source.type);if("undefined"!=typeof Hls)try{if(Hls.isSupported()){const hls=new Hls({maxBufferLength:30,maxMaxBufferLength:60,enableWorker:!0});hls.on(Hls.Events.ERROR,function(event,data){if(console.warn("HLS error:",data),data.fatal)switch(data.type){case Hls.ErrorTypes.NETWORK_ERROR:console.log("Fatal network error, trying to recover..."),hls.startLoad();break;case Hls.ErrorTypes.MEDIA_ERROR:console.log("Fatal media error, trying to recover..."),hls.recoverMediaError();break;default:console.log("Fatal error, cannot recover. Falling back to MP4"),hls.destroy(),mp4Source&&(video.src=mp4Source.src,video.load())}});try{hls.loadSource(hlsSource.src),hls.attachMedia(video),hls.on(Hls.Events.MANIFEST_PARSED,function(){video.autoplay&&video.play().catch(error=>console.warn("Auto-play failed:",error))})}catch(e){console.error("Error initializing HLS:",e),mp4Source&&(video.src=mp4Source.src,video.load())}}else console.warn("HLS.js is not supported in this browser, falling back to MP4"),mp4Source&&(video.src=mp4Source.src,video.load())}catch(e){console.error("Error with HLS.js:",e),mp4Source&&(video.src=mp4Source.src,video.load())}else console.warn("HLS.js is not loaded, falling back to MP4"),mp4Source&&(video.src=mp4Source.src,video.load())}}}setTimeout(function(){videos.forEach(setupHlsJs)},100)}});