@remotion/player 4.0.505 → 4.0.507

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePlayerMethods = void 0;
4
+ const react_1 = require("react");
5
+ const remotion_1 = require("remotion");
6
+ const emitter_context_js_1 = require("./emitter-context.js");
7
+ const timeline_imperative_context_js_1 = require("./timeline-imperative-context.js");
8
+ const usePlayerMethods = () => {
9
+ var _a;
10
+ const setFrame = remotion_1.Internals.Timeline.useTimelineSetFrame();
11
+ const setTimelinePosition = remotion_1.Internals.Timeline.useTimelineSetFrame();
12
+ const { setPlaying } = (0, react_1.useContext)(remotion_1.Internals.SetTimelineContext);
13
+ const timelineImperativeContext = (0, react_1.useContext)((0, timeline_imperative_context_js_1.getTimelineImperativeContext)());
14
+ const audioContext = (0, react_1.useContext)(remotion_1.Internals.SharedAudioContext);
15
+ const audioTagsContext = (0, react_1.useContext)(remotion_1.Internals.SharedAudioTagsContext);
16
+ const environment = (0, remotion_1.useRemotionEnvironment)();
17
+ const video = remotion_1.Internals.useVideo();
18
+ const config = remotion_1.Internals.useUnsafeVideoConfig();
19
+ const emitter = (0, react_1.useContext)(emitter_context_js_1.PlayerEventEmitterContext);
20
+ const playStart = (0, react_1.useRef)(0);
21
+ const fallbackFrame = (0, react_1.useRef)(null);
22
+ if (!timelineImperativeContext) {
23
+ throw new Error('Timeline imperative context is not available. This hook must be used inside a <Player> or the Remotion Studio.');
24
+ }
25
+ if (!emitter) {
26
+ throw new TypeError('Expected Player event emitter context');
27
+ }
28
+ const bufferingContext = (0, react_1.useContext)(remotion_1.Internals.BufferingContextReact);
29
+ if (!bufferingContext) {
30
+ throw new Error('Missing the buffering context. Most likely you have a Remotion version mismatch.');
31
+ }
32
+ const getCurrentFrame = (0, react_1.useCallback)(() => {
33
+ var _a, _b, _c;
34
+ if (!video) {
35
+ return ((_a = fallbackFrame.current) !== null && _a !== void 0 ? _a : (typeof window === 'undefined'
36
+ ? 0
37
+ : ((_b = window.remotion_initialFrame) !== null && _b !== void 0 ? _b : 0)));
38
+ }
39
+ const unclamped = (_c = timelineImperativeContext.frameRef.current[video.id]) !== null && _c !== void 0 ? _c : (environment.isPlayer
40
+ ? 0
41
+ : remotion_1.Internals.Timeline.getFrameForComposition(video.id));
42
+ return remotion_1.Internals.Timeline.clampFrameToCompositionRange(unclamped, video.durationInFrames);
43
+ }, [environment.isPlayer, timelineImperativeContext, video]);
44
+ const seek = (0, react_1.useCallback)((newFrame) => {
45
+ const frameToSeekTo = config
46
+ ? remotion_1.Internals.TimelinePosition.clampFrameToCompositionRange(newFrame, config.durationInFrames)
47
+ : Math.max(0, newFrame);
48
+ fallbackFrame.current = frameToSeekTo;
49
+ if (video === null || video === void 0 ? void 0 : video.id) {
50
+ if (timelineImperativeContext.frameRef.current[video.id] !== frameToSeekTo) {
51
+ timelineImperativeContext.frameRef.current = {
52
+ ...timelineImperativeContext.frameRef.current,
53
+ [video.id]: frameToSeekTo,
54
+ };
55
+ }
56
+ setTimelinePosition((currentFrames) => currentFrames[video.id] === frameToSeekTo
57
+ ? currentFrames
58
+ : { ...currentFrames, [video.id]: frameToSeekTo });
59
+ }
60
+ emitter.dispatchSeek(frameToSeekTo);
61
+ }, [
62
+ config,
63
+ emitter,
64
+ setTimelinePosition,
65
+ timelineImperativeContext,
66
+ video === null || video === void 0 ? void 0 : video.id,
67
+ ]);
68
+ const play = (0, react_1.useCallback)((e) => {
69
+ var _a;
70
+ if (timelineImperativeContext.imperativePlaying.current) {
71
+ return;
72
+ }
73
+ const lastFrameForPlayback = ((_a = config === null || config === void 0 ? void 0 : config.durationInFrames) !== null && _a !== void 0 ? _a : 1) - 1;
74
+ if (getCurrentFrame() === lastFrameForPlayback) {
75
+ seek(0);
76
+ }
77
+ audioContext === null || audioContext === void 0 ? void 0 : audioContext.resume();
78
+ /**
79
+ * Play silent audio tags to warm them up for autoplay
80
+ */
81
+ if (audioTagsContext && audioTagsContext.numberOfAudioTags > 0 && e) {
82
+ audioTagsContext.playAllAudios();
83
+ }
84
+ /**
85
+ * Play audios and videos directly here so they can benefit from
86
+ * being triggered by a click
87
+ */
88
+ timelineImperativeContext.audioAndVideoTags.current.forEach((tag) => tag.play('player play() was called and playing audio from a click'));
89
+ timelineImperativeContext.imperativePlaying.current = true;
90
+ setPlaying(true);
91
+ playStart.current = getCurrentFrame();
92
+ emitter.dispatchPlay();
93
+ }, [
94
+ audioContext,
95
+ audioTagsContext,
96
+ config === null || config === void 0 ? void 0 : config.durationInFrames,
97
+ emitter,
98
+ getCurrentFrame,
99
+ seek,
100
+ setPlaying,
101
+ timelineImperativeContext,
102
+ ]);
103
+ const pause = (0, react_1.useCallback)(() => {
104
+ if (timelineImperativeContext.imperativePlaying.current) {
105
+ timelineImperativeContext.imperativePlaying.current = false;
106
+ setPlaying(false);
107
+ emitter.dispatchPause();
108
+ audioContext === null || audioContext === void 0 ? void 0 : audioContext.suspend();
109
+ }
110
+ }, [audioContext, emitter, setPlaying, timelineImperativeContext]);
111
+ const pauseAndReturnToPlayStart = (0, react_1.useCallback)(() => {
112
+ if (timelineImperativeContext.imperativePlaying.current) {
113
+ timelineImperativeContext.imperativePlaying.current = false;
114
+ fallbackFrame.current = playStart.current;
115
+ if (config) {
116
+ timelineImperativeContext.frameRef.current = {
117
+ ...timelineImperativeContext.frameRef.current,
118
+ [config.id]: playStart.current,
119
+ };
120
+ setTimelinePosition((currentFrames) => ({
121
+ ...currentFrames,
122
+ [config.id]: playStart.current,
123
+ }));
124
+ setPlaying(false);
125
+ emitter.dispatchPause();
126
+ }
127
+ }
128
+ }, [
129
+ config,
130
+ emitter,
131
+ setPlaying,
132
+ setTimelinePosition,
133
+ timelineImperativeContext,
134
+ ]);
135
+ const videoId = video === null || video === void 0 ? void 0 : video.id;
136
+ const lastFrame = ((_a = config === null || config === void 0 ? void 0 : config.durationInFrames) !== null && _a !== void 0 ? _a : 1) - 1;
137
+ const frameBack = (0, react_1.useCallback)((frames) => {
138
+ var _a, _b;
139
+ if (!videoId) {
140
+ return null;
141
+ }
142
+ if (timelineImperativeContext.imperativePlaying.current) {
143
+ return;
144
+ }
145
+ const previousFrame = (_b = (_a = timelineImperativeContext.frameRef.current[videoId]) !== null && _a !== void 0 ? _a : window.remotion_initialFrame) !== null && _b !== void 0 ? _b : 0;
146
+ const newFrame = Math.max(0, previousFrame - frames);
147
+ if (previousFrame === newFrame) {
148
+ return;
149
+ }
150
+ timelineImperativeContext.frameRef.current = {
151
+ ...timelineImperativeContext.frameRef.current,
152
+ [videoId]: newFrame,
153
+ };
154
+ setFrame((currentFrames) => currentFrames[videoId] === newFrame
155
+ ? currentFrames
156
+ : { ...currentFrames, [videoId]: newFrame });
157
+ }, [setFrame, timelineImperativeContext, videoId]);
158
+ const frameForward = (0, react_1.useCallback)((frames) => {
159
+ var _a, _b;
160
+ if (!videoId) {
161
+ return null;
162
+ }
163
+ if (timelineImperativeContext.imperativePlaying.current) {
164
+ return;
165
+ }
166
+ const previousFrame = (_b = (_a = timelineImperativeContext.frameRef.current[videoId]) !== null && _a !== void 0 ? _a : window.remotion_initialFrame) !== null && _b !== void 0 ? _b : 0;
167
+ const newFrame = Math.min(lastFrame, previousFrame + frames);
168
+ if (previousFrame === newFrame) {
169
+ return;
170
+ }
171
+ timelineImperativeContext.frameRef.current = {
172
+ ...timelineImperativeContext.frameRef.current,
173
+ [videoId]: newFrame,
174
+ };
175
+ setFrame((currentFrames) => currentFrames[videoId] === newFrame
176
+ ? currentFrames
177
+ : { ...currentFrames, [videoId]: newFrame });
178
+ }, [lastFrame, setFrame, timelineImperativeContext, videoId]);
179
+ const toggle = (0, react_1.useCallback)((e) => {
180
+ if (timelineImperativeContext.imperativePlaying.current) {
181
+ pause();
182
+ }
183
+ else {
184
+ play(e);
185
+ }
186
+ }, [pause, play, timelineImperativeContext]);
187
+ const isPlaying = (0, react_1.useCallback)(() => {
188
+ return timelineImperativeContext.imperativePlaying.current;
189
+ }, [timelineImperativeContext]);
190
+ const isBuffering = (0, react_1.useCallback)(() => {
191
+ return bufferingContext.buffering.current;
192
+ }, [bufferingContext.buffering]);
193
+ return (0, react_1.useMemo)(() => {
194
+ return {
195
+ frameBack,
196
+ frameForward,
197
+ emitter,
198
+ play,
199
+ pause,
200
+ seek,
201
+ getCurrentFrame,
202
+ isPlaying,
203
+ isBuffering,
204
+ pauseAndReturnToPlayStart,
205
+ toggle,
206
+ };
207
+ }, [
208
+ emitter,
209
+ frameBack,
210
+ frameForward,
211
+ getCurrentFrame,
212
+ isBuffering,
213
+ isPlaying,
214
+ pause,
215
+ pauseAndReturnToPlayStart,
216
+ play,
217
+ seek,
218
+ toggle,
219
+ ]);
220
+ };
221
+ exports.usePlayerMethods = usePlayerMethods;