UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.useMediaInTimeline = void 0;
4const react_1 = require("react");
5const use_audio_frame_1 = require("./audio/use-audio-frame");
6const CompositionManager_1 = require("./CompositionManager");
7const get_asset_file_name_1 = require("./get-asset-file-name");
8const nonce_1 = require("./nonce");
9const play_and_handle_not_allowed_error_1 = require("./play-and-handle-not-allowed-error");
10const sequencing_1 = require("./sequencing");
11const timeline_position_state_1 = require("./timeline-position-state");
12const use_video_config_1 = require("./use-video-config");
13const volume_prop_1 = require("./volume-prop");
14const didWarn = {};
15const warnOnce = (message) => {
16 if (didWarn[message]) {
17 return;
18 }
19 console.warn(message);
20 didWarn[message] = true;
21};
22const useMediaInTimeline = ({ volume, mediaVolume, mediaRef, src, mediaType, }) => {
23 const videoConfig = (0, use_video_config_1.useVideoConfig)();
24 const { rootId, audioAndVideoTags } = (0, react_1.useContext)(timeline_position_state_1.TimelineContext);
25 const parentSequence = (0, react_1.useContext)(sequencing_1.SequenceContext);
26 const actualFrom = parentSequence
27 ? parentSequence.relativeFrom + parentSequence.cumulatedFrom
28 : 0;
29 const startsAt = (0, use_audio_frame_1.useMediaStartsAt)();
30 const { registerSequence, unregisterSequence } = (0, react_1.useContext)(CompositionManager_1.CompositionManager);
31 const [id] = (0, react_1.useState)(() => String(Math.random()));
32 const [initialVolume] = (0, react_1.useState)(() => volume);
33 const nonce = (0, nonce_1.useNonce)();
34 const duration = (() => {
35 return parentSequence
36 ? Math.min(parentSequence.durationInFrames, videoConfig.durationInFrames)
37 : videoConfig.durationInFrames;
38 })();
39 const doesVolumeChange = typeof volume === 'function';
40 const volumes = (0, react_1.useMemo)(() => {
41 if (typeof volume === 'number') {
42 return volume;
43 }
44 return new Array(Math.max(0, duration + startsAt))
45 .fill(true)
46 .map((_, i) => {
47 return (0, volume_prop_1.evaluateVolume)({
48 frame: i + startsAt,
49 volume,
50 mediaVolume,
51 });
52 })
53 .join(',');
54 }, [duration, startsAt, volume, mediaVolume]);
55 (0, react_1.useEffect)(() => {
56 if (typeof volume === 'number' && volume !== initialVolume) {
57 warnOnce(`Remotion: The ${mediaType} with src ${src} has changed it's volume. Prefer the callback syntax for setting volume to get better timeline display: https://www.remotion.dev/docs/using-audio/#controlling-volume`);
58 }
59 }, [initialVolume, mediaType, src, volume]);
60 (0, react_1.useEffect)(() => {
61 var _a;
62 if (!mediaRef.current) {
63 return;
64 }
65 if (!src) {
66 throw new Error('No src passed');
67 }
68 registerSequence({
69 type: mediaType,
70 src,
71 id,
72 // TODO: Cap to media duration
73 duration,
74 from: 0,
75 parent: (_a = parentSequence === null || parentSequence === void 0 ? void 0 : parentSequence.id) !== null && _a !== void 0 ? _a : null,
76 displayName: (0, get_asset_file_name_1.getAssetDisplayName)(src),
77 rootId,
78 volume: volumes,
79 showInTimeline: true,
80 nonce,
81 startMediaFrom: 0 - startsAt,
82 doesVolumeChange,
83 showLoopTimesInTimeline: undefined,
84 });
85 return () => {
86 unregisterSequence(id);
87 };
88 }, [
89 actualFrom,
90 duration,
91 id,
92 parentSequence,
93 src,
94 registerSequence,
95 rootId,
96 unregisterSequence,
97 videoConfig,
98 volumes,
99 doesVolumeChange,
100 nonce,
101 mediaRef,
102 mediaType,
103 startsAt,
104 ]);
105 (0, react_1.useEffect)(() => {
106 const tag = {
107 id,
108 play: () => (0, play_and_handle_not_allowed_error_1.playAndHandleNotAllowedError)(mediaRef, mediaType),
109 };
110 audioAndVideoTags.current.push(tag);
111 return () => {
112 audioAndVideoTags.current = audioAndVideoTags.current.filter((a) => a.id !== id);
113 };
114 }, [audioAndVideoTags, id, mediaRef, mediaType]);
115};
116exports.useMediaInTimeline = useMediaInTimeline;
117//# sourceMappingURL=use-media-in-timeline.js.map
\No newline at end of file