@mediafox/react
Version:
React hooks for MediaFox media player
2 lines (1 loc) • 3.57 kB
JavaScript
var m=Object.create;var{getPrototypeOf:E,defineProperty:B,getOwnPropertyNames:w}=Object;var O=Object.prototype.hasOwnProperty;var v=(x,I,F)=>{F=x!=null?m(E(x)):{};let U=I||!x||!x.__esModule?B(F,"default",{value:x,enumerable:!0}):F;for(let V of w(x))if(!O.call(U,V))B(U,V,{get:()=>x[V],enumerable:!0});return U};var S=((x)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(x,{get:(I,F)=>(typeof require<"u"?require:I)[F]}):x)(function(x){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+x+'" is not supported')});import{useCallback as T,useEffect as G,useMemo as k,useRef as j,useSyncExternalStore as C}from"react";function h(x={}){let I=j(null),F=j(null),U=j(new Set),V=k(()=>({renderTarget:x.renderTarget,audioContext:x.audioContext,volume:x.volume,muted:x.muted,playbackRate:x.playbackRate,autoplay:x.autoplay,preload:x.preload,crossOrigin:x.crossOrigin,maxCacheSize:x.maxCacheSize,renderer:x.renderer}),[x.renderTarget,x.audioContext,x.volume,x.muted,x.playbackRate,x.autoplay,x.preload,x.crossOrigin,x.maxCacheSize,x.renderer]);G(()=>{import("@mediafox/core").then(({MediaFox:L})=>{let M=new L(V);I.current=M;let P=M.subscribe((D)=>{F.current=D;for(let Q of U.current)Q()});F.current=M.getState();for(let D of U.current)D();return()=>{P.unsubscribe(),M.destroy(),I.current=null,F.current=null}})},[V]),G(()=>{let L=I.current;if(!L)return;let M=[],P=(D,Q)=>{if(Q)L.on(D,Q),M.push([D,Q])};return P("loadstart",x.onLoadStart),P("loadedmetadata",x.onLoadedMetadata),P("loadeddata",x.onLoadedData),P("canplay",x.onCanPlay),P("canplaythrough",x.onCanPlayThrough),P("play",x.onPlay),P("pause",x.onPause),P("playing",x.onPlaying),P("ended",x.onEnded),P("timeupdate",x.onTimeUpdate),P("durationchange",x.onDurationChange),P("volumechange",x.onVolumeChange),P("ratechange",x.onRateChange),P("seeking",x.onSeeking),P("seeked",x.onSeeked),P("waiting",x.onWaiting),P("progress",x.onProgress),P("error",x.onError),P("warning",x.onWarning),P("trackchange",x.onTrackChange),P("statechange",x.onStateChange),P("rendererchange",x.onRendererChange),P("rendererfallback",x.onRendererFallback),()=>{for(let[D,Q]of M)L.off(D,Q)}},[x.onLoadStart,x.onLoadedMetadata,x.onLoadedData,x.onCanPlay,x.onCanPlayThrough,x.onPlay,x.onPause,x.onPlaying,x.onEnded,x.onTimeUpdate,x.onDurationChange,x.onVolumeChange,x.onRateChange,x.onSeeking,x.onSeeked,x.onWaiting,x.onProgress,x.onError,x.onWarning,x.onTrackChange,x.onStateChange,x.onRendererChange,x.onRendererFallback]);let J=T((L)=>{return U.current.add(L),()=>{U.current.delete(L)}},[]),q=T(()=>F.current,[]),A=C(J,q,q),K=T(async(L,M)=>{if(!I.current)throw Error("Player not initialized");return I.current.load(L,M)},[]),N=T(async()=>{if(!I.current)throw Error("Player not initialized");return I.current.play()},[]),W=T(()=>{if(!I.current)throw Error("Player not initialized");I.current.pause()},[]),X=T(async(L,M)=>{if(!I.current)throw Error("Player not initialized");return I.current.seek(L,M)},[]),Y=T(async()=>{if(!I.current)throw Error("Player not initialized");return I.current.stop()},[]),Z=T(async(L)=>{if(!I.current)throw Error("Player not initialized");return I.current.screenshot(L)},[]),_=T(async(L)=>{if(!I.current)throw Error("Player not initialized");return I.current.setRenderTarget(L)},[]),$=T(async(L)=>{if(!I.current)throw Error("Player not initialized");return I.current.switchRenderer(L)},[]),z=A?.rendererType??null;return{player:I.current,state:A,load:K,play:N,pause:W,seek:X,stop:Y,screenshot:Z,setRenderTarget:_,switchRenderer:$,rendererType:z}}export{h as useMediaFox};