UNPKG

@vime/core

Version:

Customizable, extensible, accessible and framework agnostic media player.

1 lines 7.44 kB
import{r as t,c as i,h as s}from"./p-aa8acb66.js";import{b as e,a as h}from"./p-e3b0430a.js";import{c as a,h as o,m as n,u as r,o as c,v as u}from"./p-b018976f.js";import{M as d}from"./p-8b74fa0e.js";import{V as p}from"./p-415151d7.js";import{w as m,c as l}from"./p-17652948.js";import{w as b}from"./p-ad08a3bd.js";import"./p-d84b1c8a.js";import"./p-121aab6e.js";import"./p-8acb8eb5.js";import"./p-cbbce638.js";import"./p-152748b8.js";const f=t=>{switch(t){case"unknown":return;case"tiny":return"144p";case"small":return"240p";case"medium":return"360p";case"large":return"480p";case"hd720":return"720p";case"hd1080":return"1080p";case"highres":return"1440p";case"max":return"2160p";default:return}};var v=function(t,i,s,e){return new(s||(s=Promise))((function(h,a){function o(t){try{r(e.next(t))}catch(t){a(t)}}function n(t){try{r(e.throw(t))}catch(t){a(t)}}function r(t){var i;t.done?h(t.value):(i=t.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,n)}r((e=e.apply(t,i||[])).next())}))};const y=new Map,g=class{constructor(s){t(this,s),this.vmLoadStart=i(this,"vmLoadStart",7),this.defaultInternalState={},this.internalState={paused:!0,duration:0,seeking:!1,playbackReady:!1,playbackStarted:!1,currentTime:0,lastTimeUpdate:0,playbackRate:1,state:-1},this.embedSrc="",this.mediaTitle="",this.cookies=!1,this.showFullscreenControl=!0,this.language="en",this.autoplay=!1,this.controls=!1,this.loop=!1,this.muted=!1,this.playsinline=!1,a(this),m(this),b(this)}onVideoIdChange(){this.videoId?(this.embedSrc=`${this.getOrigin()}/embed/${this.videoId}`,this.fetchPosterURL=this.findPosterURL()):this.embedSrc=""}onCustomPosterChange(){this.dispatch("currentPoster",this.poster)}connectedCallback(){this.dispatch=l(this),this.dispatch("viewType",p.Video),this.onVideoIdChange(),this.defaultInternalState=Object.assign({},this.internalState)}componentWillLoad(){this.initialMuted=this.muted}getAdapter(){return v(this,void 0,void 0,(function*(){const t=/(?:youtu\.be|youtube|youtube\.com|youtube-nocookie\.com)\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=|)((?:\w|-){11})/;return{getInternalPlayer:()=>v(this,void 0,void 0,(function*(){return this.embed})),play:()=>v(this,void 0,void 0,(function*(){this.remoteControl("playVideo")})),pause:()=>v(this,void 0,void 0,(function*(){this.remoteControl("pauseVideo")})),canPlay:i=>v(this,void 0,void 0,(function*(){return o(i)&&t.test(i)})),setCurrentTime:t=>v(this,void 0,void 0,(function*(){t!==this.internalState.currentTime&&this.remoteControl("seekTo",t)})),setMuted:t=>v(this,void 0,void 0,(function*(){this.remoteControl(t?"mute":"unMute")})),setVolume:t=>v(this,void 0,void 0,(function*(){this.remoteControl("setVolume",t)})),canSetPlaybackRate:()=>v(this,void 0,void 0,(function*(){return!0})),setPlaybackRate:t=>v(this,void 0,void 0,(function*(){this.remoteControl("setPlaybackRate",t)}))}}))}getOrigin(){return this.cookies?"https://www.youtube.com":"https://www.youtube-nocookie.com"}getPreconnections(){return[this.getOrigin(),"https://www.google.com","https://googleads.g.doubleclick.net","https://static.doubleclick.net","https://s.ytimg.com","https://i.ytimg.com"]}remoteControl(t,i){return this.embed.postMessage({event:"command",func:t,args:i?[i]:void 0})}buildParams(){return{enablejsapi:1,cc_lang_pref:this.language,hl:this.language,fs:this.showFullscreenControl?1:0,controls:this.controls?1:0,disablekb:this.controls?0:1,iv_load_policy:this.controls?1:3,mute:this.initialMuted?1:0,playsinline:this.playsinline?1:0,autoplay:this.autoplay?1:0}}onEmbedSrcChange(){this.vmLoadStart.emit(),this.dispatch("viewType",p.Video)}onEmbedLoaded(){window.setTimeout((()=>this.embed.postMessage({event:"listening"})),100)}findPosterURL(){return v(this,void 0,void 0,(function*(){if(y.has(this.videoId))return y.get(this.videoId);const t=t=>`https://i.ytimg.com/vi/${this.videoId}/${t}.jpg`;return e(t("maxresdefault"),121).catch((()=>e(t("sddefault"),121))).catch((()=>e(t("hqdefault"),121))).then((t=>{const i=t.src;return y.set(this.videoId,i),i}))}))}onCued(){this.internalState.playbackReady||(this.internalState=Object.assign({},this.defaultInternalState),this.dispatch("currentSrc",this.embedSrc),this.dispatch("mediaType",d.Video),this.fetchPosterURL.then((t=>{var i;this.dispatch("currentPoster",null!==(i=this.poster)&&void 0!==i?i:t),this.dispatch("playbackReady",!0)})),this.internalState.playbackReady=!0)}onPlayerStateChange(t){if(this.internalState.playbackReady&&-1===t)return this.internalState.paused=!0,this.internalState.playbackStarted=!1,this.dispatch("buffering",!1),this.dispatch("paused",!0),void this.dispatch("playbackStarted",!1);const i=1===t,s=3===t;switch(this.dispatch("buffering",s),this.internalState.paused&&(s||i)&&(this.internalState.paused=!1,this.dispatch("paused",!1),this.internalState.playbackStarted||(this.dispatch("playbackStarted",!0),this.internalState.playbackStarted=!0)),t){case 5:this.onCued();break;case 1:this.onCued(),this.dispatch("playing",!0);break;case 2:this.internalState.paused=!0,this.dispatch("paused",!0);break;case 0:this.loop?window.setTimeout((()=>{this.remoteControl("playVideo")}),150):(this.dispatch("playbackEnded",!0),this.internalState.paused=!0,this.dispatch("paused",!0))}this.internalState.state=t}calcCurrentTime(t){let i=t;if(0===this.internalState.state)return this.internalState.duration;if(1===this.internalState.state){const t=(Date.now()/1e3-this.defaultInternalState.lastTimeUpdate)*this.internalState.playbackRate;t>0&&(i+=Math.min(t,1))}return i}onTimeChange(t){const i=this.calcCurrentTime(t);this.dispatch("currentTime",i),Math.abs(this.internalState.currentTime-i)>1.5&&(this.internalState.seeking=!0,this.dispatch("seeking",!0)),this.internalState.currentTime=i}onBufferedChange(t){this.dispatch("buffered",t),this.internalState.seeking&&t>this.internalState.currentTime&&window.setTimeout((()=>{this.internalState.seeking=!1,this.dispatch("seeking",!1)}),this.internalState.paused?100:0)}onEmbedMessage(t){const i=t.detail,{info:s}=i;s&&(n(s.videoData)&&this.dispatch("mediaTitle",s.videoData.title),r(s.duration)&&(this.internalState.duration=s.duration,this.dispatch("duration",s.duration)),c(s.availablePlaybackRates)&&this.dispatch("playbackRates",s.availablePlaybackRates),r(s.playbackRate)&&(this.internalState.playbackRate=s.playbackRate,this.dispatch("playbackRate",s.playbackRate)),r(s.currentTime)&&this.onTimeChange(s.currentTime),r(s.currentTimeLastUpdated)&&(this.internalState.lastTimeUpdate=s.currentTimeLastUpdated),r(s.videoLoadedFraction)&&this.onBufferedChange(s.videoLoadedFraction*this.internalState.duration),r(s.volume)&&this.dispatch("volume",s.volume),u(s.muted)&&this.dispatch("muted",s.muted),c(s.availableQualityLevels)&&this.dispatch("playbackQualities",s.availableQualityLevels.map((t=>f(t)))),o(s.playbackQuality)&&this.dispatch("playbackQuality",f(s.playbackQuality)),r(s.playerState)&&this.onPlayerStateChange(s.playerState))}render(){return s("vm-embed",{embedSrc:this.embedSrc,mediaTitle:this.mediaTitle,origin:this.getOrigin(),params:this.buildParams(),decoder:h,preconnections:this.getPreconnections(),onVmEmbedLoaded:this.onEmbedLoaded.bind(this),onVmEmbedMessage:this.onEmbedMessage.bind(this),onVmEmbedSrcChange:this.onEmbedSrcChange.bind(this),ref:t=>{this.embed=t}})}static get watchers(){return{cookies:["onVideoIdChange"],videoId:["onVideoIdChange"],poster:["onCustomPosterChange"]}}};g.style=":host{z-index:var(--vm-media-z-index)}";export{g as vm_youtube}