@remotion/player 4.0.535 → 4.0.537

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.
@@ -30,13 +30,14 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
30
30
  const setFrameFromPlayback = remotion_1.Internals.Timeline.useTimelineSetFrameWithoutSeek();
31
31
  const sharedAudioContext = (0, react_2.useContext)(remotion_1.Internals.SharedAudioContext);
32
32
  const { setPlayerMuted } = (0, react_2.useContext)(remotion_1.Internals.SetMediaVolumeContext);
33
- const { isBuffering, subscribeBuffering } = (0, react_2.useContext)(remotion_1.Internals.SetTimelineContext);
33
+ const { isBuffering, subscribeBuffering, seek } = (0, react_2.useContext)(remotion_1.Internals.SetTimelineContext);
34
34
  const logLevel = remotion_1.Internals.useLogLevel();
35
35
  // requestAnimationFrame() does not work if the tab is not active.
36
36
  // This means that audio will keep playing even if it has ended.
37
37
  // In that case, we use setTimeout() instead.
38
38
  const isBackgroundedRef = (0, is_backgrounded_js_1.useIsBackgrounded)();
39
39
  const lastTimeUpdateTimestamp = (0, react_2.useRef)(0);
40
+ const playbackClockRef = (0, react_2.useRef)(null);
40
41
  (0, browser_mediasession_js_1.useBrowserMediaSession)({
41
42
  browserMediaControlsBehavior,
42
43
  playbackRate,
@@ -112,13 +113,28 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
112
113
  ]);
113
114
  (0, react_2.useEffect)(() => {
114
115
  var _a;
116
+ var _b;
115
117
  if (!config) {
116
118
  return;
117
119
  }
118
120
  if (!playing) {
121
+ playbackClockRef.current = null;
119
122
  (_a = sharedAudioContext === null || sharedAudioContext === void 0 ? void 0 : sharedAudioContext.suspend) === null || _a === void 0 ? void 0 : _a.call(sharedAudioContext);
120
123
  return;
121
124
  }
125
+ // Either carry over the previous clock or start a fresh one.
126
+ const previousClock = playbackClockRef.current;
127
+ const clock = previousClock !== null &&
128
+ previousClock.fps === config.fps &&
129
+ previousClock.playbackRate === playbackRate
130
+ ? previousClock
131
+ : {
132
+ startedTime: performance.now(),
133
+ framesAdvanced: 0,
134
+ fps: config.fps,
135
+ playbackRate,
136
+ };
137
+ playbackClockRef.current = clock;
122
138
  if ((sharedAudioContext === null || sharedAudioContext === void 0 ? void 0 : sharedAudioContext._experimentalKeepAudioContextAlive) &&
123
139
  sharedAudioContext.audioContext &&
124
140
  !muted) {
@@ -145,8 +161,8 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
145
161
  let hasBeenStopped = false;
146
162
  let audioContextFailed = false;
147
163
  let reqAnimFrameCall = null;
148
- let startedTime = performance.now();
149
- let framesAdvanced = 0;
164
+ let playbackFrame = getCurrentFrame();
165
+ let lastSeekRevision = (_b = seek === null || seek === void 0 ? void 0 : seek.revision.current) !== null && _b !== void 0 ? _b : null;
150
166
  const cancelQueuedFrame = () => {
151
167
  if (reqAnimFrameCall !== null) {
152
168
  if (reqAnimFrameCall.type === 'raf') {
@@ -163,6 +179,7 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
163
179
  };
164
180
  const callback = () => {
165
181
  var _a, _b;
182
+ var _c;
166
183
  if (hasBeenStopped) {
167
184
  return;
168
185
  }
@@ -173,10 +190,16 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
173
190
  if (!muted && !audioContextFailed && !isBuffering()) {
174
191
  (_b = sharedAudioContext === null || sharedAudioContext === void 0 ? void 0 : sharedAudioContext.resume) === null || _b === void 0 ? void 0 : _b.call(sharedAudioContext);
175
192
  }
176
- const time = performance.now() - startedTime;
193
+ const time = performance.now() - clock.startedTime;
177
194
  const actualLastFrame = outFrame !== null && outFrame !== void 0 ? outFrame : config.durationInFrames - 1;
178
195
  const actualFirstFrame = inFrame !== null && inFrame !== void 0 ? inFrame : 0;
179
- const currentFrame = getCurrentFrame();
196
+ const seekRevision = (_c = seek === null || seek === void 0 ? void 0 : seek.revision.current) !== null && _c !== void 0 ? _c : null;
197
+ if (seekRevision !== lastSeekRevision) {
198
+ playbackFrame = getCurrentFrame();
199
+ lastSeekRevision = seekRevision;
200
+ }
201
+ // React may not have committed the previous playback update yet.
202
+ const currentFrame = playbackFrame;
180
203
  const { nextFrame, framesToAdvance, hasEnded } = (0, calculate_next_frame_js_1.calculateNextFrame)({
181
204
  time,
182
205
  currentFrame,
@@ -184,17 +207,18 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
184
207
  fps: config.fps,
185
208
  actualFirstFrame,
186
209
  actualLastFrame,
187
- framesAdvanced,
210
+ framesAdvanced: clock.framesAdvanced,
188
211
  shouldLoop: loop,
189
212
  });
190
- framesAdvanced += framesToAdvance;
213
+ clock.framesAdvanced += framesToAdvance;
191
214
  // A late callback can skip the endpoint. Hold that exact frame on end.
192
215
  const frameToDisplay = hasEnded && !moveToBeginningWhenEnded
193
216
  ? playbackRate > 0
194
217
  ? actualLastFrame
195
218
  : actualFirstFrame
196
219
  : nextFrame;
197
- if (frameToDisplay !== getCurrentFrame() && !isBuffering()) {
220
+ if (frameToDisplay !== currentFrame && !isBuffering()) {
221
+ playbackFrame = frameToDisplay;
198
222
  setFrameFromPlayback((c) => ({ ...c, [config.id]: frameToDisplay }));
199
223
  }
200
224
  if (hasEnded) {
@@ -224,8 +248,9 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
224
248
  sharedAudioContext === null || sharedAudioContext === void 0 ? void 0 : sharedAudioContext.suspend();
225
249
  setPlayerMuted(true);
226
250
  }
227
- startedTime = performance.now();
228
- framesAdvanced = 0;
251
+ // The audio context is being re-established; start a fresh clock.
252
+ clock.startedTime = performance.now();
253
+ clock.framesAdvanced = 0;
229
254
  queueNextFrame();
230
255
  });
231
256
  return;
@@ -244,8 +269,9 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
244
269
  (sharedAudioContext === null || sharedAudioContext === void 0 ? void 0 : sharedAudioContext._experimentalKeepAudioContextAlive)) {
245
270
  sharedAudioContext.resume();
246
271
  }
247
- startedTime = performance.now();
248
- framesAdvanced = 0;
272
+ // Buffered media became reachable; start a fresh clock.
273
+ clock.startedTime = performance.now();
274
+ clock.framesAdvanced = 0;
249
275
  queueNextFrame();
250
276
  });
251
277
  return;
@@ -293,6 +319,7 @@ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, ou
293
319
  sharedAudioContext,
294
320
  setPlayerMuted,
295
321
  subscribeBuffering,
322
+ seek,
296
323
  logLevel,
297
324
  muted,
298
325
  ]);
@@ -960,10 +960,11 @@ var usePlayback = ({
960
960
  const setFrameFromPlayback = Internals6.Timeline.useTimelineSetFrameWithoutSeek();
961
961
  const sharedAudioContext = useContext3(Internals6.SharedAudioContext);
962
962
  const { setPlayerMuted } = useContext3(Internals6.SetMediaVolumeContext);
963
- const { isBuffering, subscribeBuffering } = useContext3(Internals6.SetTimelineContext);
963
+ const { isBuffering, subscribeBuffering, seek } = useContext3(Internals6.SetTimelineContext);
964
964
  const logLevel = Internals6.useLogLevel();
965
965
  const isBackgroundedRef = useIsBackgrounded();
966
966
  const lastTimeUpdateTimestamp = useRef4(0);
967
+ const playbackClockRef = useRef4(null);
967
968
  useBrowserMediaSession({
968
969
  browserMediaControlsBehavior,
969
970
  playbackRate,
@@ -1038,9 +1039,18 @@ var usePlayback = ({
1038
1039
  return;
1039
1040
  }
1040
1041
  if (!playing) {
1042
+ playbackClockRef.current = null;
1041
1043
  sharedAudioContext?.suspend?.();
1042
1044
  return;
1043
1045
  }
1046
+ const previousClock = playbackClockRef.current;
1047
+ const clock = previousClock !== null && previousClock.fps === config.fps && previousClock.playbackRate === playbackRate ? previousClock : {
1048
+ startedTime: performance.now(),
1049
+ framesAdvanced: 0,
1050
+ fps: config.fps,
1051
+ playbackRate
1052
+ };
1053
+ playbackClockRef.current = clock;
1044
1054
  if (sharedAudioContext?._experimentalKeepAudioContextAlive && sharedAudioContext.audioContext && !muted) {
1045
1055
  const changed = setGlobalTimeAnchor({
1046
1056
  audioContext: sharedAudioContext.audioContext,
@@ -1057,8 +1067,8 @@ var usePlayback = ({
1057
1067
  let hasBeenStopped = false;
1058
1068
  let audioContextFailed = false;
1059
1069
  let reqAnimFrameCall = null;
1060
- let startedTime = performance.now();
1061
- let framesAdvanced = 0;
1070
+ let playbackFrame = getCurrentFrame();
1071
+ let lastSeekRevision = seek?.revision.current ?? null;
1062
1072
  const cancelQueuedFrame = () => {
1063
1073
  if (reqAnimFrameCall !== null) {
1064
1074
  if (reqAnimFrameCall.type === "raf") {
@@ -1083,10 +1093,15 @@ var usePlayback = ({
1083
1093
  if (!muted && !audioContextFailed && !isBuffering()) {
1084
1094
  sharedAudioContext?.resume?.();
1085
1095
  }
1086
- const time = performance.now() - startedTime;
1096
+ const time = performance.now() - clock.startedTime;
1087
1097
  const actualLastFrame = outFrame ?? config.durationInFrames - 1;
1088
1098
  const actualFirstFrame = inFrame ?? 0;
1089
- const currentFrame = getCurrentFrame();
1099
+ const seekRevision = seek?.revision.current ?? null;
1100
+ if (seekRevision !== lastSeekRevision) {
1101
+ playbackFrame = getCurrentFrame();
1102
+ lastSeekRevision = seekRevision;
1103
+ }
1104
+ const currentFrame = playbackFrame;
1090
1105
  const { nextFrame, framesToAdvance, hasEnded } = calculateNextFrame({
1091
1106
  time,
1092
1107
  currentFrame,
@@ -1094,12 +1109,13 @@ var usePlayback = ({
1094
1109
  fps: config.fps,
1095
1110
  actualFirstFrame,
1096
1111
  actualLastFrame,
1097
- framesAdvanced,
1112
+ framesAdvanced: clock.framesAdvanced,
1098
1113
  shouldLoop: loop
1099
1114
  });
1100
- framesAdvanced += framesToAdvance;
1115
+ clock.framesAdvanced += framesToAdvance;
1101
1116
  const frameToDisplay = hasEnded && !moveToBeginningWhenEnded ? playbackRate > 0 ? actualLastFrame : actualFirstFrame : nextFrame;
1102
- if (frameToDisplay !== getCurrentFrame() && !isBuffering()) {
1117
+ if (frameToDisplay !== currentFrame && !isBuffering()) {
1118
+ playbackFrame = frameToDisplay;
1103
1119
  setFrameFromPlayback((c) => ({ ...c, [config.id]: frameToDisplay }));
1104
1120
  }
1105
1121
  if (hasEnded) {
@@ -1125,8 +1141,8 @@ var usePlayback = ({
1125
1141
  sharedAudioContext?.suspend();
1126
1142
  setPlayerMuted(true);
1127
1143
  }
1128
- startedTime = performance.now();
1129
- framesAdvanced = 0;
1144
+ clock.startedTime = performance.now();
1145
+ clock.framesAdvanced = 0;
1130
1146
  queueNextFrame();
1131
1147
  });
1132
1148
  return;
@@ -1143,8 +1159,8 @@ var usePlayback = ({
1143
1159
  if (!muted && !audioContextFailed && sharedAudioContext?._experimentalKeepAudioContextAlive) {
1144
1160
  sharedAudioContext.resume();
1145
1161
  }
1146
- startedTime = performance.now();
1147
- framesAdvanced = 0;
1162
+ clock.startedTime = performance.now();
1163
+ clock.framesAdvanced = 0;
1148
1164
  queueNextFrame();
1149
1165
  });
1150
1166
  return;
@@ -1189,6 +1205,7 @@ var usePlayback = ({
1189
1205
  sharedAudioContext,
1190
1206
  setPlayerMuted,
1191
1207
  subscribeBuffering,
1208
+ seek,
1192
1209
  logLevel,
1193
1210
  muted
1194
1211
  ]);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "url": "https://github.com/remotion-dev/remotion/tree/main/packages/player"
4
4
  },
5
5
  "name": "@remotion/player",
6
- "version": "4.0.535",
6
+ "version": "4.0.537",
7
7
  "description": "React component for embedding a Remotion preview into your app",
8
8
  "main": "dist/cjs/index.js",
9
9
  "types": "dist/cjs/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "license": "SEE LICENSE IN LICENSE.md",
37
37
  "dependencies": {
38
- "remotion": "4.0.535"
38
+ "remotion": "4.0.537"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": ">=16.8.0",
@@ -49,7 +49,7 @@
49
49
  "react-dom": "19.2.3",
50
50
  "webpack": "5.105.0",
51
51
  "zod": "4.5.4",
52
- "@remotion/eslint-config-internal": "4.0.535",
52
+ "@remotion/eslint-config-internal": "4.0.537",
53
53
  "eslint": "9.19.0",
54
54
  "@typescript/native-preview": "7.0.0-dev.20260217.1"
55
55
  },
@@ -1,11 +0,0 @@
1
- import type { Context } from 'react';
2
- import React from 'react';
3
- import type { TimelineImperativeContextValue } from 'remotion';
4
- export declare const getTimelineImperativeContext: () => Context<TimelineImperativeContextValue | null>;
5
- export declare const CoreTimelineImperativeContextProvider: React.FC<{
6
- readonly children: React.ReactNode;
7
- readonly value: TimelineImperativeContextValue;
8
- }>;
9
- export declare const TimelineImperativeCompatibilityProvider: React.FC<{
10
- readonly children: React.ReactNode;
11
- }>;
@@ -1,76 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.TimelineImperativeCompatibilityProvider = exports.CoreTimelineImperativeContextProvider = exports.getTimelineImperativeContext = void 0;
37
- const jsx_runtime_1 = require("react/jsx-runtime");
38
- const react_1 = __importStar(require("react"));
39
- const remotion_1 = require("remotion");
40
- const PlayerTimelineImperativeContext = react_1.default.createContext(null);
41
- const getCoreTimelineImperativeContext = () => {
42
- return remotion_1.Internals.TimelineImperativeContext;
43
- };
44
- const getTimelineImperativeContext = () => {
45
- var _a;
46
- return (_a = getCoreTimelineImperativeContext()) !== null && _a !== void 0 ? _a : PlayerTimelineImperativeContext;
47
- };
48
- exports.getTimelineImperativeContext = getTimelineImperativeContext;
49
- const CoreTimelineImperativeContextProvider = ({ children, value }) => {
50
- const TimelineImperativeContext = getCoreTimelineImperativeContext();
51
- if (!TimelineImperativeContext) {
52
- return children;
53
- }
54
- return (jsx_runtime_1.jsx(TimelineImperativeContext.Provider, { value: value, children: children }));
55
- };
56
- exports.CoreTimelineImperativeContextProvider = CoreTimelineImperativeContextProvider;
57
- const LegacyTimelineImperativeContextProvider = ({ children }) => {
58
- const timelineContext = remotion_1.Internals.Timeline.useTimelineContext();
59
- const frameRef = (0, react_1.useRef)(timelineContext.frame);
60
- frameRef.current = timelineContext.frame;
61
- const value = (0, react_1.useMemo)(() => {
62
- return {
63
- frameRef,
64
- imperativePlaying: timelineContext.imperativePlaying,
65
- audioAndVideoTags: timelineContext.audioAndVideoTags,
66
- };
67
- }, [timelineContext.audioAndVideoTags, timelineContext.imperativePlaying]);
68
- return (jsx_runtime_1.jsx(PlayerTimelineImperativeContext.Provider, { value: value, children: children }));
69
- };
70
- const TimelineImperativeCompatibilityProvider = ({ children }) => {
71
- if (getCoreTimelineImperativeContext()) {
72
- return children;
73
- }
74
- return (jsx_runtime_1.jsx(LegacyTimelineImperativeContextProvider, { children: children }));
75
- };
76
- exports.TimelineImperativeCompatibilityProvider = TimelineImperativeCompatibilityProvider;
@@ -1,21 +0,0 @@
1
- import type { SyntheticEvent } from 'react';
2
- import type { PlayerEmitter } from './event-emitter.js';
3
- type UsePlayerMethods = {
4
- frameBack: (frames: number) => void;
5
- frameForward: (frames: number) => void;
6
- isLastFrame: boolean;
7
- isFirstFrame: boolean;
8
- emitter: PlayerEmitter;
9
- playing: boolean;
10
- play: (e?: SyntheticEvent | PointerEvent) => void;
11
- pause: () => void;
12
- pauseAndReturnToPlayStart: () => void;
13
- seek: (newFrame: number) => void;
14
- getCurrentFrame: () => number;
15
- isPlaying: () => boolean;
16
- hasPlayed: boolean;
17
- isBuffering: () => boolean;
18
- toggle: (e?: SyntheticEvent | PointerEvent) => void;
19
- };
20
- export declare const usePlayer: () => UsePlayerMethods;
21
- export {};
@@ -1,195 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.usePlayer = 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 usePlayer = () => {
8
- var _a;
9
- const [playing, setPlaying, imperativePlaying] = remotion_1.Internals.Timeline.usePlayingState();
10
- const [hasPlayed, setHasPlayed] = (0, react_1.useState)(false);
11
- const frame = remotion_1.Internals.Timeline.useTimelinePosition();
12
- const playStart = (0, react_1.useRef)(frame);
13
- const setFrame = remotion_1.Internals.Timeline.useTimelineSetFrame();
14
- const setTimelinePosition = remotion_1.Internals.Timeline.useTimelineSetFrame();
15
- const audioContext = (0, react_1.useContext)(remotion_1.Internals.SharedAudioContext);
16
- const audioTagsContext = (0, react_1.useContext)(remotion_1.Internals.SharedAudioTagsContext);
17
- const { audioAndVideoTags } = remotion_1.Internals.useTimelineContext();
18
- const frameRef = (0, react_1.useRef)(frame);
19
- frameRef.current = frame;
20
- const video = remotion_1.Internals.useVideo();
21
- const config = remotion_1.Internals.useUnsafeVideoConfig();
22
- const emitter = (0, react_1.useContext)(emitter_context_js_1.PlayerEventEmitterContext);
23
- const lastFrame = ((_a = config === null || config === void 0 ? void 0 : config.durationInFrames) !== null && _a !== void 0 ? _a : 1) - 1;
24
- const isLastFrame = frame === lastFrame;
25
- const isFirstFrame = frame === 0;
26
- if (!emitter) {
27
- throw new TypeError('Expected Player event emitter context');
28
- }
29
- const bufferingContext = (0, react_1.useContext)(remotion_1.Internals.BufferingContextReact);
30
- if (!bufferingContext) {
31
- throw new Error('Missing the buffering context. Most likely you have a Remotion version mismatch.');
32
- }
33
- const { buffering } = bufferingContext;
34
- const seek = (0, react_1.useCallback)((newFrame) => {
35
- const frameToSeekTo = config
36
- ? remotion_1.Internals.TimelinePosition.clampFrameToCompositionRange(newFrame, config.durationInFrames)
37
- : Math.max(0, newFrame);
38
- if (video === null || video === void 0 ? void 0 : video.id) {
39
- setTimelinePosition((c) => c[video.id] === frameToSeekTo ? c : { ...c, [video.id]: frameToSeekTo });
40
- }
41
- frameRef.current = frameToSeekTo;
42
- emitter.dispatchSeek(frameToSeekTo);
43
- }, [config, emitter, setTimelinePosition, video === null || video === void 0 ? void 0 : video.id]);
44
- const play = (0, react_1.useCallback)((e) => {
45
- if (imperativePlaying.current) {
46
- return;
47
- }
48
- setHasPlayed(true);
49
- if (isLastFrame) {
50
- seek(0);
51
- }
52
- audioContext === null || audioContext === void 0 ? void 0 : audioContext.resume();
53
- /**
54
- * Play silent audio tags to warm them up for autoplay
55
- */
56
- if (audioTagsContext && audioTagsContext.numberOfAudioTags > 0 && e) {
57
- audioTagsContext.playAllAudios();
58
- }
59
- /**
60
- * Play audios and videos directly here so they can benefit from
61
- * being triggered by a click
62
- */
63
- audioAndVideoTags.current.forEach((a) => a.play('player play() was called and playing audio from a click'));
64
- imperativePlaying.current = true;
65
- setPlaying(true);
66
- playStart.current = frameRef.current;
67
- emitter.dispatchPlay();
68
- }, [
69
- imperativePlaying,
70
- isLastFrame,
71
- audioContext,
72
- audioTagsContext,
73
- setPlaying,
74
- emitter,
75
- seek,
76
- audioAndVideoTags,
77
- ]);
78
- const pause = (0, react_1.useCallback)(() => {
79
- if (imperativePlaying.current) {
80
- imperativePlaying.current = false;
81
- setPlaying(false);
82
- emitter.dispatchPause();
83
- audioContext === null || audioContext === void 0 ? void 0 : audioContext.suspend();
84
- }
85
- }, [emitter, imperativePlaying, setPlaying, audioContext]);
86
- const pauseAndReturnToPlayStart = (0, react_1.useCallback)(() => {
87
- if (imperativePlaying.current) {
88
- imperativePlaying.current = false;
89
- frameRef.current = playStart.current;
90
- if (config) {
91
- setTimelinePosition((c) => ({
92
- ...c,
93
- [config.id]: playStart.current,
94
- }));
95
- setPlaying(false);
96
- emitter.dispatchPause();
97
- }
98
- }
99
- }, [config, emitter, imperativePlaying, setPlaying, setTimelinePosition]);
100
- const videoId = video === null || video === void 0 ? void 0 : video.id;
101
- const frameBack = (0, react_1.useCallback)((frames) => {
102
- if (!videoId) {
103
- return null;
104
- }
105
- if (imperativePlaying.current) {
106
- return;
107
- }
108
- setFrame((c) => {
109
- var _a, _b;
110
- const prevFrame = (_b = (_a = c[videoId]) !== null && _a !== void 0 ? _a : window.remotion_initialFrame) !== null && _b !== void 0 ? _b : 0;
111
- const newFrame = Math.max(0, prevFrame - frames);
112
- if (prevFrame === newFrame) {
113
- return c;
114
- }
115
- return {
116
- ...c,
117
- [videoId]: newFrame,
118
- };
119
- });
120
- }, [imperativePlaying, setFrame, videoId]);
121
- const frameForward = (0, react_1.useCallback)((frames) => {
122
- if (!videoId) {
123
- return null;
124
- }
125
- if (imperativePlaying.current) {
126
- return;
127
- }
128
- setFrame((c) => {
129
- var _a, _b;
130
- const prevFrame = (_b = (_a = c[videoId]) !== null && _a !== void 0 ? _a : window.remotion_initialFrame) !== null && _b !== void 0 ? _b : 0;
131
- const newFrame = Math.min(lastFrame, prevFrame + frames);
132
- if (prevFrame === newFrame) {
133
- return c;
134
- }
135
- return {
136
- ...c,
137
- [videoId]: newFrame,
138
- };
139
- });
140
- }, [videoId, imperativePlaying, lastFrame, setFrame]);
141
- const toggle = (0, react_1.useCallback)((e) => {
142
- if (imperativePlaying.current) {
143
- pause();
144
- }
145
- else {
146
- play(e);
147
- }
148
- }, [imperativePlaying, pause, play]);
149
- const isPlaying = (0, react_1.useCallback)(() => {
150
- return imperativePlaying.current;
151
- }, [imperativePlaying]);
152
- const getCurrentFrame = (0, react_1.useCallback)(() => {
153
- return frameRef.current;
154
- }, [frameRef]);
155
- const isBuffering = (0, react_1.useCallback)(() => {
156
- return buffering.current;
157
- }, [buffering]);
158
- const returnValue = (0, react_1.useMemo)(() => {
159
- return {
160
- frameBack,
161
- frameForward,
162
- isLastFrame,
163
- emitter,
164
- playing,
165
- play,
166
- pause,
167
- seek,
168
- isFirstFrame,
169
- getCurrentFrame,
170
- isPlaying,
171
- isBuffering,
172
- pauseAndReturnToPlayStart,
173
- hasPlayed,
174
- toggle,
175
- };
176
- }, [
177
- emitter,
178
- frameBack,
179
- frameForward,
180
- hasPlayed,
181
- isFirstFrame,
182
- isLastFrame,
183
- getCurrentFrame,
184
- pause,
185
- pauseAndReturnToPlayStart,
186
- play,
187
- playing,
188
- seek,
189
- toggle,
190
- isPlaying,
191
- isBuffering,
192
- ]);
193
- return returnValue;
194
- };
195
- exports.usePlayer = usePlayer;