-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast.min.js
1 lines (1 loc) · 9.1 KB
/
cast.min.js
1
"use strict";var head=document.getElementsByTagName("head")[0],script=document.createElement("script");function handler(){$pdk.bind({id:"player",controls:"full",logLevel:"event"});var e=!1;function t(){var t=document.createElement("div");t.id="cast-container";var o=document.createElement("div");if(o.id="button-container",o.innerHTML='<google-cast-launcher id="castbutton"></google-cast-launcher>',t.appendChild(o),!e){var r,a;if(a=Array.prototype.slice.call(document.querySelectorAll(".tpPlayer .tpControls .tpRow .tpFullscreen")).pop()){for(r=a;!r.classList.contains("tpRow")&&!r.classList.contains("tpControls");)r=r.parentNode;r.classList.contains("tpRow")&&(r.insertBefore(t,a),e=!0)}else(r=Array.prototype.slice.call(document.querySelectorAll(".tpPlayer .tpControls .tpRow")).pop())&&(r.appendChild(t),e=!0);r&&(a=r.querySelectorAll(".tpFullscreen"))}}$pdk.controller.addPlayerCard("samples","red","<div class='container'><div class='card-container'><h1 style='color: white; margin:auto;font-size:4vw'>Casting to <span id='deviceName'></span></h1><div class='controls'><div style='display:flex;margin-top:3px;margin:auto'><div id='customPlay' class='custom-icon'></div><div id='customPause' class='custom-icon'></div><div style='display:flex'><div id='customMute' class='custom-icon'></div><div id='customVolume' class='custom-icon'></div><input type='range' min='1' max='100' value='0' class='slider' orient='vertical' step='0.1' id='volumeSlider'></div></div><div class='slidecontainer'><input type='range' min='1' max='100' value='0' class='slider' step='0.1' id='myRange'></div><div id='customTime' style='color:white;margin:auto'><p style='margin-bottom:0px;margin-top:0px;font-size:2vw' id='customTimerText'>00:00:00/00:00:00</p></div><div style='margin:auto'><div style='margin-right:25px'><div id='customCast' class='custom-icon'></div></div></div></div>","player",null,function(e,t){var o={};return o.controller=t,o.element=e,o.show=function(e){this.vars=e,e.deviceName&&(this.element.querySelector("#deviceName").textContent=e.deviceName)},o.hide=function(){console.log("Stop casting!")},o}),$pdk.controller.addEventListener("OnMediaLoadStart",function(e){n.playingURL=e.data.URL,n.isLive=e.data.isLive,t()}),$pdk.controller.addEventListener("OnMediaPlaying",function(e){t(),n.playingTime=e.data.currentTimeAggregate,n.isLive=e.data.isLive}),$pdk.controller.addEventListener("OnReleaseRequested",function(e){n.releaseURL=e.data.releaseURL}),$pdk.controller.addEventListener("OnPlayerReady",function(e){n.releaseURL=e.data.releaseURL}),$(document).on("click touchstart","#customPlay",function(){cast.framework.CastContext.getInstance().getCurrentSession().getMediaSession()?($(this).hide(),$("#customPause").show(),n.remotePlayer.isPaused&&n.remotePlayerController.playOrPause()):n.castMediaToDevice()}),$(document).on("click touchstart","#customPause",function(){$(this).hide(),$("#customPlay").show(),n.remotePlayer.isPaused||n.remotePlayerController.playOrPause()}),$(document).on("click touchstart","#customMute",function(){"none"==$("#volumeSlider").css("display")?$("#volumeSlider").show():$("#volumeSlider").hide()}),$(document).on("click touchstart","#customVolume",function(){"none"==$("#volumeSlider").css("display")?$("#volumeSlider").show():$("#volumeSlider").hide()}),$(document).on("click touchstart","#customCast",function(){cast.framework.CastContext.getInstance().getCurrentSession().endSession(!0)});var o=0;function r(e,t){e.val(t);var o=(e.val()-e.attr("min"))/(e.attr("max")-e.attr("min"));e.css("background","-webkit-gradient(linear, left top, right top, color-stop(0, #04b1ff), color-stop("+o+", #04b1ff), color-stop("+o+", #ffffff))")}function a(e){var t=new Date(null);return t.setSeconds(e),t.toISOString().substr(11,8)}function i(){var e=cast.framework.CastContext.getInstance().getCurrentSession(),t=e.getMediaSession();if(t){var o=t.getEstimatedTime();n.currentCastingTime=o;var i=a(n.remotePlayer.duration),s=a(o);n.isLive?$("#customTimerText").text("LIVE"):$("#customTimerText").text(s+"/"+i);var l=o/n.remotePlayer.duration*100;r($("#myRange"),l)}else clearInterval(n.playerTimer),n.playingTime=0,e.endSession(!0)}$("#myRange").on("mousedown touchstart",function(e){o=$(this).val()}),$("#myRange").on("mouseup touchend",function(){var e=$(this).val();if(e!==o&&!n.isLive){var t=cast.framework.CastContext.getInstance().getCurrentSession().getMediaSession();if(t){clearInterval(n.playerTimer);var a=(e-$(this).attr("min"))/($(this).attr("max")-$(this).attr("min")),s=new chrome.cast.media.SeekRequest;s.currentTime=n.remotePlayer.duration*a,t.seek(s,function(){n.playerTimer=setInterval(i,1e3),console.log("Seek completed!")},function(e){console.log(e)})}else r($("#myRange"),0)}}),$("#myRange").on("input propertychange",function(){var e=($(this).val()-$(this).attr("min"))/($(this).attr("max")-$(this).attr("min"));$(this).css("background","-webkit-gradient(linear, left top, right top, color-stop(0, #04b1ff), color-stop("+e+", #04b1ff), color-stop("+e+", #ffffff))")}),$("#volumeSlider").on("input propertychange",function(){var e=($(this).val()-$(this).attr("min"))/($(this).attr("max")-$(this).attr("min"));$(this).css("background","-webkit-gradient(linear, left top, right top, color-stop(0, #04b1ff), color-stop("+e+", #04b1ff), color-stop("+e+", #ffffff))"),0==e?($("#customVolume").hide(),$("#customMute").show()):($("#customMute").hide(),$("#customVolume").show()),n.remotePlayer.volumeLevel=e,n.remotePlayerController.setVolumeLevel()});var s=function(){this.remotePlayer=null,this.remotePlayerController=null,this.playingURL=null,this.playingTime=null,this.playerTimer=null,this.releaseURL=null,this.isNewMediaLoaded=!1,this.currentCastingTime=null,this.isLive=!1};s.prototype.initializeCastPlayer=function(){var e={};e.receiverApplicationId=chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,e.autoJoinPolicy=chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,cast.framework.CastContext.getInstance().setOptions(e),this.remotePlayer=new cast.framework.RemotePlayer,this.remotePlayerController=new cast.framework.RemotePlayerController(this.remotePlayer),this.remotePlayerController.addEventListener(cast.framework.RemotePlayerEventType.IS_CONNECTED_CHANGED,this.switchPlayer.bind(this))},s.prototype.switchPlayer=function(){cast&&cast.framework&&this.remotePlayer.isConnected?this.setupRemotePlayer():this.setupLocalPlayer()},s.prototype.setupRemotePlayer=function(){this.castMediaToDevice()},s.prototype.setupLocalPlayer=function(){n.isLive?$pdk.controller.start(n.releaseURL):n.remotePlayer.savedPlayerState.mediaInfo?($pdk.controller.seekTo(1e3*n.currentCastingTime),$pdk.controller.pause(!1)):$pdk.controller.preview(n.releaseURL),$pdk.controller.hidePlayerCard("samples","red"),clearInterval(n.playerTimer)},s.prototype.castMediaToDevice=function(){var e=cast.framework.CastContext.getInstance().getCurrentSession();if(n.playingURL){var t=new chrome.cast.media.MediaInfo(n.playingURL);t.metadata=new chrome.cast.media.GenericMediaMetadata,t.metadata.metadataType=chrome.cast.media.MetadataType.GENERIC;var o=new chrome.cast.media.LoadRequest(t);e.loadMedia(o).then(function(){if(n.remotePlayer.volumeLevel=.5,n.remotePlayerController.setVolumeLevel(),$("#customPlay").hide(),$("#customPause").show(),$("#customMute").hide(),$("#customVolume").show(),$("#volumeSlider").hide(),r($("#volumeSlider"),50),$pdk.controller.showPlayerCard("samples","red",null,{deviceName:e.getCastDevice().friendlyName}),n.playerTimer=setInterval(i,1e3),$pdk.controller.pause(!0),!n.isLive){var t=e.getMediaSession(),o=new chrome.cast.media.SeekRequest;o.currentTime=n.playingTime/1e3,t.seek(o,function(){console.log("Seek completed!")},function(e){console.log(e)})}},function(e){console.log("Remote media load error: "+s.getErrorMessage(e))})}},s.getErrorMessage=function(e){switch(e.code){case chrome.cast.ErrorCode.API_NOT_INITIALIZED:return"The API is not initialized."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.CANCEL:return"The operation was canceled by the user"+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.CHANNEL_ERROR:return"A channel to the receiver is not available."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.EXTENSION_MISSING:return"The Cast extension is not available."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.INVALID_PARAMETER:return"The parameters to the operation were not valid."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.RECEIVER_UNAVAILABLE:return"No receiver was compatible with the session request."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.SESSION_ERROR:return"A session could not be created, or a session was invalid."+(e.description?" :"+e.description:"");case chrome.cast.ErrorCode.TIMEOUT:return"The operation timed out."+(e.description?" :"+e.description:"")}};var n=new s;window.__onGCastApiAvailable=function(e){e&&n.initializeCastPlayer()};var l=document.createElement("script");l.setAttribute("type","text/javascript"),l.src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1",document.body.appendChild(l)}script.type="text/javascript",script.src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js",script.onreadystatechange=handler,script.onload=handler,head.appendChild(script);