1 line
No EOL
7.3 KiB
JavaScript
1 line
No EOL
7.3 KiB
JavaScript
import appRouter from"./../approuter.js";import events from"./../emby-apiclient/events.js";import MethodTimer from"./../common/methodtimer.js";function createMediaElement(instance,options){let dlg=document.querySelector(".youtubePlayerContainer");return dlg?dlg.classList.add("youtubePlayerContainer"):((dlg=document.createElement("div")).classList.add("youtubePlayerContainer"),dlg.innerHTML='<div id="player"></div>',document.body.insertBefore(dlg,document.body.firstChild),instance.videoDialog=dlg),dlg.querySelector("#player")}function onVideoResize(){let instance=this;setTimeout(function(){var player=instance.currentYoutubePlayer,dlg=instance.videoDialog;player&&dlg&&player.setSize(dlg.offsetWidth,dlg.offsetHeight)},100)}function clearTimeUpdateInterval(instance){instance.timeUpdateInterval&&instance.timeUpdateInterval.destroy(),instance.timeUpdateInterval=null}function onEndedInternal(instance,triggerStopped){clearTimeUpdateInterval(instance);var resizeListener=instance.resizeListener;resizeListener&&(window.removeEventListener("resize",resizeListener),window.removeEventListener("orientationChange",resizeListener),instance.resizeListener=null),triggerStopped&&events.trigger(instance,"stopped",[{}]),instance.currentYoutubePlayer&&instance.currentYoutubePlayer.destroy(),instance.currentYoutubePlayer=null}function onTimeUpdate(e){events.trigger(this,"timeupdate")}function setCurrentSrc(instance,elem,options,signal){if(signal.aborted){let reason=signal.reason;return reason||((reason=new Error("Aborted")).name="AbortError"),Promise.reject(reason)}return options.fullscreen&&appRouter.showVideoOsd(),new Promise(function(resolve,reject){if(signal.aborted){let reason=signal.reason;reason||((reason=new Error("Aborted")).name="AbortError"),void reject(reason)}else{console.log("youtube playing: "+options.url);let params=new URLSearchParams(options.url.split("?")[1]);var tag,firstScriptTag;window.onYouTubeIframeAPIReady=function(){if(signal.aborted){let reason=signal.reason;reason||((reason=new Error("Aborted")).name="AbortError"),void reject(reason)}else{instance.currentYoutubePlayer=new YT.Player("player",{height:instance.videoDialog.offsetHeight,width:instance.videoDialog.offsetWidth,videoId:params.get("v"),events:{onReady:function(event){if(signal.aborted){stopInternal(instance,!0,!1);let reason=signal.reason;reason||((reason=new Error("Aborted")).name="AbortError"),void reject(reason)}else event.target.playVideo()},onStateChange:function(event){if(event.data===YT.PlayerState.PLAYING){var rejectFn=reject;if(reject=null,resolve){if(signal.aborted){if(stopInternal(instance,!0,!1),rejectFn){let reason=signal.reason;reason||((reason=new Error("Aborted")).name="AbortError"),rejectFn(reason)}return}resolve(),resolve=null}instance.timeUpdateInterval||(instance.timeUpdateInterval=new MethodTimer({onInterval:onTimeUpdate.bind(instance),timeoutMs:500,type:"interval"}))}else event.data===YT.PlayerState.ENDED?onEndedInternal(instance,!0):event.data===YT.PlayerState.PAUSED&&events.trigger(instance,"pause")},onError:function(event){console.log("youtubeplayer, received error code during playback : "+event.data);event=reject;event?(reject=null,event()):events.trigger(instance,"error")}},playerVars:{controls:0,enablejsapi:1,modestbranding:1,rel:0,showinfo:0,fs:0,playsinline:1}});let resizeListener=instance.resizeListener;resizeListener?window.removeEventListener("resize",resizeListener):resizeListener=instance.resizeListener=onVideoResize.bind(instance),window.addEventListener("resize",resizeListener),window.removeEventListener("orientationChange",resizeListener),window.addEventListener("orientationChange",resizeListener)}},window.YT?window.onYouTubeIframeAPIReady():((tag=document.createElement("script")).src="https://www.youtube.com/iframe_api",(firstScriptTag=document.getElementsByTagName("script")[0]).parentNode.insertBefore(tag,firstScriptTag))}})}function YoutubePlayer(){this.name="Youtube Player",this.type="mediaplayer",this.id="youtubeplayer",this.priority=1}function stopInternal(instance,destroyPlayer,triggerStopped){var currentYoutubePlayer=instance.currentYoutubePlayer;currentYoutubePlayer&&(currentYoutubePlayer.stopVideo&¤tYoutubePlayer.stopVideo(),onEndedInternal(instance,triggerStopped)),destroyPlayer&&instance.destroy()}require(["css!modules/youtubeplayer/style.css"]),YoutubePlayer.prototype.play=function(options,signal){return setCurrentSrc(this,createMediaElement(this,options),options,signal)},YoutubePlayer.prototype.isPlaying=function(){return null!=this.currentYoutubePlayer},YoutubePlayer.prototype.stop=function(destroyPlayer){return stopInternal(this,destroyPlayer,!0),Promise.resolve()},YoutubePlayer.prototype.destroy=function(){var dlg=this.videoDialog;dlg&&(this.videoDialog=null,dlg.parentNode.removeChild(dlg))},YoutubePlayer.prototype.canPlayMediaType=function(mediaType){switch(mediaType){case"Audio":case"Video":return!0;default:return!1}},YoutubePlayer.prototype.canPlayItem=function(item){return!1},YoutubePlayer.prototype.canPlayUrl=function(url){return url.toLowerCase().includes("youtube.com")},YoutubePlayer.prototype.getDeviceProfile=function(){return Promise.resolve({})},YoutubePlayer.prototype.setSubtitleStreamIndex=function(index){},YoutubePlayer.prototype.canSetAudioStreamIndex=function(){return!1},YoutubePlayer.prototype.setAudioStreamIndex=function(index){},YoutubePlayer.prototype.currentTime=function(val){var currentYoutubePlayer=this.currentYoutubePlayer;if(currentYoutubePlayer&¤tYoutubePlayer.getCurrentTime){if(null==val)return 1e3*currentYoutubePlayer.getCurrentTime();currentYoutubePlayer.seekTo(val/1e3,!0)}},YoutubePlayer.prototype.duration=function(val){var currentYoutubePlayer=this.currentYoutubePlayer;return currentYoutubePlayer&¤tYoutubePlayer.getDuration?1e3*currentYoutubePlayer.getDuration():null},YoutubePlayer.prototype.pause=function(){var currentYoutubePlayer=this.currentYoutubePlayer;if(currentYoutubePlayer&¤tYoutubePlayer.pauseVideo){currentYoutubePlayer.pauseVideo();let instance=this;setTimeout(function(){events.trigger(instance,"pause")},200)}},YoutubePlayer.prototype.unpause=function(){var currentYoutubePlayer=this.currentYoutubePlayer;if(currentYoutubePlayer&¤tYoutubePlayer.playVideo){currentYoutubePlayer.playVideo();let instance=this;setTimeout(function(){events.trigger(instance,"unpause")},200)}},YoutubePlayer.prototype.paused=function(){var currentYoutubePlayer=this.currentYoutubePlayer;return!(!currentYoutubePlayer||!currentYoutubePlayer.getPlayerState)&&2===currentYoutubePlayer.getPlayerState()},YoutubePlayer.prototype.volume=function(val){return null!=val?this.setVolume(val):this.getVolume()},YoutubePlayer.prototype.setVolume=function(val){var currentYoutubePlayer=this.currentYoutubePlayer;currentYoutubePlayer&&null!=val&¤tYoutubePlayer.setVolume(val)},YoutubePlayer.prototype.getVolume=function(){var currentYoutubePlayer=this.currentYoutubePlayer;if(currentYoutubePlayer&¤tYoutubePlayer.getVolume)return currentYoutubePlayer.getVolume()},YoutubePlayer.prototype.setMute=function(mute){var currentYoutubePlayer=this.currentYoutubePlayer;mute?currentYoutubePlayer&¤tYoutubePlayer.mute&¤tYoutubePlayer.mute():currentYoutubePlayer&¤tYoutubePlayer.unMute&¤tYoutubePlayer.unMute()},YoutubePlayer.prototype.isMuted=function(){var currentYoutubePlayer=this.currentYoutubePlayer;if(currentYoutubePlayer&¤tYoutubePlayer.isMuted)return currentYoutubePlayer.isMuted()};export default YoutubePlayer; |