jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/htmlaudioplayer/plugin.js

1 line
7.7 KiB
JavaScript
Raw Normal View History

2017-05-30 12:33:56 -07:00
define(["events","browser","require","apphost","appSettings","./../htmlvideoplayer/htmlmediahelper"],function(events,browser,require,appHost,appSettings,htmlMediaHelper){"use strict";function getDefaultProfile(){return new Promise(function(resolve,reject){require(["browserdeviceprofile"],function(profileBuilder){resolve(profileBuilder({}))})})}function fade(elem,startingVolume){var newVolume=Math.max(0,startingVolume-.15);return console.log("fading volume to "+newVolume),elem.volume=newVolume,newVolume<=0?Promise.resolve():new Promise(function(resolve,reject){cancelFadeTimeout(),fadeTimeout=setTimeout(function(){fade(elem,newVolume).then(resolve,reject)},100)})}function cancelFadeTimeout(){var timeout=fadeTimeout;timeout&&(clearTimeout(timeout),fadeTimeout=null)}function supportsFade(){return!browser.tv}function requireHlsPlayer(callback){require(["hlsjs"],function(hls){window.Hls=hls,callback()})}function enableHlsPlayer(url,item,mediaSource,mediaType){return htmlMediaHelper.enableHlsPlayer(item,mediaSource,mediaType)?url.indexOf(".m3u8")!==-1?Promise.resolve():new Promise(function(resolve,reject){require(["fetchHelper"],function(fetchHelper){fetchHelper.ajax({url:url,type:"HEAD"}).then(function(response){var contentType=(response.headers.get("Content-Type")||"").toLowerCase();"application/x-mpegurl"===contentType?resolve():reject()},reject)})}):Promise.reject()}function HtmlAudioPlayer(){function setCurrentSrc(elem,options){elem.removeEventListener("error",onError);var val=options.url;console.log("playing url: "+val);var seconds=(options.playerStartPositionTicks||0)/1e7;seconds&&(val+="#t="+seconds),htmlMediaHelper.destroyHlsPlayer(self),self._currentPlayOptions=options;var crossOrigin=htmlMediaHelper.getCrossOriginValue(options.mediaSource);return crossOrigin&&(elem.crossOrigin=crossOrigin),enableHlsPlayer(val,options.item,options.mediaSource,"Audio").then(function(){return new Promise(function(resolve,reject){requireHlsPlayer(function(){var hls=new Hls({manifestLoadingTimeOut:2e4});hls.loadSource(val),hls.attachMedia(elem),htmlMediaHelper.bindEventsToHlsPlayer(self,hls,elem,onError,resolve,reject),self._hlsPlayer=hls,self._currentSrc=val})})},function(){elem.autoplay=!0;options.mimeType;return htmlMediaHelper.applySrc(elem,val,options).then(function(){return self._currentSrc=val,htmlMediaHelper.playWithPromise(elem,onError)})})}function createMediaElement(){var elem=self._mediaElement;return elem?elem:(elem=document.querySelector(".mediaPlayerAudio"),elem||(elem=document.createElement("audio"),elem.classList.add("mediaPlayerAudio"),elem.classList.add("hide"),document.body.appendChild(elem)),elem.volume=htmlMediaHelper.getSavedVolume(),elem.addEventListener("timeupdate",onTimeUpdate),elem.addEventListener("ended",onEnded),elem.addEventListener("volumechange",onVolumeChange),elem.addEventListener("pause",onPause),elem.addEventListener("playing",onPlaying),elem.addEventListener("play",onPlay),self._mediaElement=elem,elem)}function onEnded(){htmlMediaHelper.onEndedInternal(self,this,onError)}function onTimeUpdate(){var time=this.currentTime;self._currentTime=time,events.trigger(self,"timeupdate")}function onVolumeChange(){fadeTimeout||(htmlMediaHelper.saveVolume(this.volume),events.trigger(self,"volumechange"))}function onPlaying(e){self._started||(self._started=!0,this.removeAttribute("controls"),htmlMediaHelper.seekOnPlaybackStart(self,e.target,self._currentPlayOptions.playerStartPositionTicks)),events.trigger(self,"playing")}function onPlay(e){events.trigger(self,"unpause")}function onPause(){events.trigger(self,"pause")}function onError(){var errorCode=this.error?this.error.code||0:0;console.log("Media element error code: "+errorCode.toString());var type;switch(errorCode){case 1:return;case 2:type="network";break;case 3:if(self._hlsPlayer)return void htmlMediaHelper.handleMediaError(self);type="mediadecodeerror";break;case 4:type="medianotsupported";break;default:return}htmlMediaHelper.onErrorInternal(self,type)}function onDocumentClick(){document.removeEventListener("click",onDocumentClick);var elem=document.create