Skip to content

🎥 Make videos programmatically with React

simplify

Jonny Burgereb46d3fe539b
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index e28754da9a..227028a18f 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -206,11 +206,14 @@ const AudioForPreviewAssertedShowing: React.FC<
if (!sharedAudioContext.audioSyncAnchor) return;
if (!sharedAudioContext.scheduleAudioNode) return;
+ const {audioContext, audioSyncAnchor, scheduleAudioNode} =
+ sharedAudioContext;
+
try {
const player = new MediaPlayer({
src: preloadedSrc,
logLevel,
- sharedAudioContext,
+ sharedAudioContext: {audioContext, audioSyncAnchor, scheduleAudioNode},
loop,
trimAfter: initialTrimAfterRef.current,
trimBefore: initialTrimBeforeRef.current,
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index cbb373d9ae..723e3ad10e 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -273,7 +273,7 @@ export class MediaPlayer {
throw new Error(`should have asserted that the time is not null`);
}
- if (audioTrack && this.sharedAudioContext?.audioContext) {
+ if (audioTrack && this.sharedAudioContext) {
const canDecode = await audioTrack.canDecode();
if (!canDecode) {
return {type: 'cannot-decode'};
@@ -307,7 +307,7 @@ export class MediaPlayer {
startFromSecond: startTime,
getIsPlaying: () => this.playing,
scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
- if (!this.sharedAudioContext?.scheduleAudioNode) {
+ if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
}
@@ -388,9 +388,7 @@ export class MediaPlayer {
}
const shouldSeekAudio =
- this.audioIteratorManager &&
- this.sharedAudioContext &&
- this.getAudioPlaybackTime() !== newTime;
+ this.audioIteratorManager && this.getAudioPlaybackTime() !== newTime;
try {
await Promise.all([
@@ -405,7 +403,7 @@ export class MediaPlayer {
playbackRate: this.playbackRate * this.globalPlaybackRate,
getIsPlaying: () => this.playing,
scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
- if (!this.sharedAudioContext?.scheduleAudioNode) {
+ if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
}
@@ -441,7 +439,7 @@ export class MediaPlayer {
this.audioIteratorManager.resumeScheduledAudioChunks({
playbackRate: this.playbackRate * this.globalPlaybackRate,
scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
- if (!this.sharedAudioContext?.scheduleAudioNode) {
+ if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
}
@@ -458,7 +456,7 @@ export class MediaPlayer {
}
if (
- this.sharedAudioContext?.audioContext &&
+ this.sharedAudioContext &&
this.sharedAudioContext.audioContext.state === 'suspended'
) {
await this.sharedAudioContext.audioContext.resume();
@@ -594,7 +592,7 @@ export class MediaPlayer {
this.audioIteratorManager.resumeScheduledAudioChunks({
playbackRate: this.playbackRate * this.globalPlaybackRate,
scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
- if (!this.sharedAudioContext?.scheduleAudioNode) {
+ if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
}
@@ -673,10 +671,7 @@ export class MediaPlayer {
}
private getAudioPlaybackTime(): number {
- if (
- !this.sharedAudioContext?.audioContext ||
- !this.sharedAudioContext.audioSyncAnchor
- ) {
+ if (!this.sharedAudioContext) {
throw new Error('Shared audio context not found');
}
@@ -707,8 +702,8 @@ export class MediaPlayer {
if (this.context && this.canvas) {
drawPreviewOverlay({
context: this.context,
- audioTime: this.sharedAudioContext?.audioContext?.currentTime ?? null,
- audioContextState: this.sharedAudioContext?.audioContext?.state ?? null,
+ audioTime: this.sharedAudioContext?.audioContext.currentTime ?? null,
+ audioContextState: this.sharedAudioContext?.audioContext.state ?? null,
audioSyncAnchor: this.sharedAudioContext?.audioSyncAnchor ?? null,
audioIteratorManager: this.audioIteratorManager,
playing: this.playing,
diff --git a/packages/media/src/shared-audio-context-for-media-player.ts b/packages/media/src/shared-audio-context-for-media-player.ts
index 6fe18c9aea..e70c5bd2b2 100644
--- a/packages/media/src/shared-audio-context-for-media-player.ts
+++ b/packages/media/src/shared-audio-context-for-media-player.ts
@@ -7,7 +7,7 @@ export type ScheduleAudioNodeOptions = {
};
export type SharedAudioContextForMediaPlayer = {
- audioContext: AudioContext | null;
- audioSyncAnchor: {value: number} | null;
- scheduleAudioNode: ((options: ScheduleAudioNodeOptions) => boolean) | null;
+ audioContext: AudioContext;
+ audioSyncAnchor: {value: number};
+ scheduleAudioNode: (options: ScheduleAudioNodeOptions) => boolean;
};
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 259787a110..975b15cd9f 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -208,13 +208,19 @@ const VideoForPreviewAssertedShowing: React.FC<
useEffect(() => {
if (!sharedAudioContext) return;
+ if (!sharedAudioContext.audioContext) return;
+ if (!sharedAudioContext.audioSyncAnchor) return;
+ if (!sharedAudioContext.scheduleAudioNode) return;
+
+ const {audioContext, audioSyncAnchor, scheduleAudioNode} =
+ sharedAudioContext;
try {
const player = new MediaPlayer({
canvas: canvasRef.current,
src: preloadedSrc,
logLevel,
- sharedAudioContext: sharedAudioContext ?? null,
+ sharedAudioContext: {audioContext, audioSyncAnchor, scheduleAudioNode},
loop,
trimAfter: initialTrimAfterRef.current,
trimBefore: initialTrimBeforeRef.current,