UNPKG

@tycoonsystems/tycoon-modules

Version:
1 lines 3.92 kB
import{handleInteractMedia,handleAnalyticsRequest,throttleFunctionCall}from"@tycoonsystems/tycoon-modules/util";import{HMSDurationToSeconds}from"@tycoonsystems/tycoon-modules/utility/utility/date";const disposePlayer=e=>{try{var t;window?.videojs?.players&&(t=window.videojs.players)&&Object.keys(t).length&&t?.[e]?.dispose&&t[e].dispose()}catch(e){}},setTimeline=(a,e)=>{console.log(a,e,window?.VTTCue);try{if(a&&e?.timeline&&window?.VTTCue){const s=a.addRemoteTextTrack({kind:"metadata",mode:"showing"},!1).track,o=e.timeline.filter(e=>"clip"===e.type),l=e?.duration?HMSDurationToSeconds(e.duration):0,d=a?.controlBar?.progressControl?.el();o.forEach((e,t)=>{var i=e.startOffset,n=e?.endOffset??(o[t+1]&&o[t+1].startOffset)?o[t+1].startOffset-1:l??0,e=e.name,n=new window.VTTCue(i,n,e);s.addCue(n),d&&a?.id_&&(0===t&&((e=document.createElement("div")).className="segmentContainer",e.id=a.id_+"_segmentContainer",d.appendChild(e)),(n=document.getElementById(a.id_+"_segmentContainer"))&&((t=document.createElement("div")).classList.add("vjs-segment-square"),e=i/l*100,t.style.left=e+"%",n.appendChild(t)),console.log("Seek",d))}),a.on("timeupdate",function(){})}}catch(e){}},initButtons=async(t,i)=>{for(let e=0;e<i.length;e++){var n,a;i[e]?.id&&!document.getElementById(i[e].id)&&((a=(n=t.current.controlBar.addChild("button")).el()).innerHTML=i[e].innerHTML??"",i[e].className&&n.addClass(i[e].className),a.id=i[e].id,i[e].btnEvent)&&(a.removeEventListener("click",i[e].btnEvent),a.addEventListener("click",i[e].btnEvent))}},initializePlayer=(t,i,n,a,e,s,o=[],l,d,r,c,y,h,u)=>{try{const w=-1<["upload","preview","simple"].indexOf(e?.type);if(window.videojs){if(console.log("Run Init",a,document.getElementById(a)),window.videojs.Vhs.GOAL_BUFFER_LENGTH=100,window.videojs.Vhs.MAX_GOAL_BUFFER_LENGTH=150,!window.videojs.players[a]||e.force)return window.videojs(a,e,async function(){s?.current&&(r&&r(!0),window.videojs.log(`Your player ${a} is ready!`),console.log(s.current),initButtons(s,o),this.on("error",e=>{console.log("Error",e,a),l&&l(e)}),this.on("ready",e=>{i?.id&&t&&n?.trying&&(handleInteractMedia(t,t.watchData,n.trying),w||setTimeline(this,i)),c&&c(e?.target?.player??this,i)}),this.on("play",e=>{console.log("Play event",e,window.isAuthorized),window.isAuthorized||w||(this.pause(),this.currentTime(0)),u&&u(e),this._recentlyPlayedVideos||(this._recentlyPlayedVideos=[]),handleRecentlyPlayed.call(this,t),y&&y(!0)}),this.on("waiting",e=>{console.log("Player is buffering"),this.lastBufferWaiting=(new Date).getTime(),setTimeout(()=>{if(w)this.play();else if(null!==this.lastBufferWaiting&&!window.imaPlaying&&window.isAuthorized&&(console.log("pl buf",t),"Live"===t?.watchData?.__typename)&&this?.play)try{this.play()}catch(e){}},5e3)}),this.on("stalled",e=>{console.log("Player is stalled")}),this.on("progress",e=>{(!this.lastProgressUpdate||this.lastProgressUpdate<(new Date).getTime()-7500)&&(l&&l(e,"update","progress"),this.lastProgressUpdate=(new Date).getTime()),null!==this.lastBufferWaiting&&(this.lastBufferWaiting=null)}),this.on("ended",e=>{console.log("Ended",a),d&&d()}))});if(document.getElementById(a)){var m=document.getElementById(a);if(console.log(`Player ${a} already initialized`,m,m?.player.hasStarted(),h),!m?.player?.hasStarted())try{try{!window?.userInteracted&&m?.player&&m.player.muted(!0)}catch(e){}m.player.play()}catch(e){console.log("Failed to play",e)}}}}catch(e){}};function handleRecentlyPlayed(e){var t;this?._recentlyPlayedVideos&&this?.src&&(t=this.src())&&-1===this._recentlyPlayedVideos.indexOf(t)&&t.match(/.+\/([A-Za-z0-9].*)-(mpd|hls)/)?.[1]&&(t=t.match(/.+\/([A-Za-z0-9].*)-(mpd|hls)/)[1],console.log(e?.watchData?.id,t,e?.watchData?.id===t),e?.watchData?.id===t)&&(throttleFunctionCall(window,"_onPlayback_analytics_watch",1500,handleAnalyticsRequest,[e,"video","watch",t]),this._recentlyPlayedVideos.push(t))}export{disposePlayer,initializePlayer,initButtons};