Skip to content

🎥 Make videos programmatically with React

better

JonnyBurger08d36435a4dd
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index bfde4a6272..16c1359b9a 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -290,7 +290,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
if (!audioPlayer) return;
if (playing && !isPlayerBuffering) {
- audioPlayer.play();
+ audioPlayer.play(currentTimeRef.current);
} else {
audioPlayer.pause();
}
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index bc31495a91..0b6b5b7787 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -386,7 +386,7 @@ export class MediaPlayer {
}
if (audioSatisfyResult.type === 'not-satisfied') {
- this.startAudioIterator(newTime, nonce);
+ await this.startAudioIterator(newTime, nonce);
return;
}
@@ -397,9 +397,9 @@ export class MediaPlayer {
const nextTime =
newTime +
// start of next frame
- 1 / this.fps +
+ (1 / this.fps) * this.playbackRate +
// need the full duration of the next frame to be queued
- 1 / this.fps;
+ (1 / this.fps) * this.playbackRate;
const nextIsAlreadyQueued = isAlreadyQueued(nextTime, queuedPeriod);
if (!nextIsAlreadyQueued) {
const audioSatisfyResult =
@@ -414,7 +414,7 @@ export class MediaPlayer {
}
if (audioSatisfyResult.type === 'not-satisfied') {
- this.startAudioIterator(nextTime, nonce);
+ await this.startAudioIterator(nextTime, nonce);
return;
}
@@ -433,10 +433,10 @@ export class MediaPlayer {
}
}
- public play(): void {
+ public play(time: number): void {
if (!this.isReady()) return;
- this.setPlaybackTime(this.getPlaybackTime());
+ this.setPlaybackTime(time);
this.playing = true;
for (const chunk of this.audioChunksForAfterResuming) {
@@ -537,16 +537,28 @@ export class MediaPlayer {
buffer: AudioBuffer,
mediaTimestamp: number,
): void {
- const targetTime = mediaTimestamp + this.audioSyncAnchor;
- const delay = targetTime - this.sharedAudioContext.currentTime;
+ // TODO: Might already be scheduled, and then the playback rate changes
+ // TODO: Playbackrate does not yet work
+ const targetTime =
+ (mediaTimestamp - (this.trimBefore ?? 0) / this.fps) / this.playbackRate;
+ const delay =
+ targetTime + this.audioSyncAnchor - this.sharedAudioContext.currentTime;
const node = this.sharedAudioContext.createBufferSource();
node.buffer = buffer;
node.playbackRate.value = this.playbackRate;
node.connect(this.gainNode!);
+ console.log(
+ 'scheduleAudioChunk',
+ mediaTimestamp,
+ targetTime,
+ delay,
+ this.trimBefore,
+ );
+
if (delay >= 0) {
- node.start(targetTime);
+ node.start(targetTime + this.audioSyncAnchor);
} else {
node.start(this.sharedAudioContext.currentTime, -delay);
}
@@ -629,14 +641,10 @@ export class MediaPlayer {
const {buffer, timestamp} = result.value;
- if (this.playing) {
- this.scheduleAudioChunk(buffer, timestamp);
- } else {
- this.audioChunksForAfterResuming.push({
- buffer,
- timestamp,
- });
- }
+ this.audioChunksForAfterResuming.push({
+ buffer,
+ timestamp,
+ });
}
delayHandle.unblock();
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 9bc7f8f323..686001fd9c 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -303,7 +303,7 @@ export const VideoForPreview: React.FC<VideoForPreviewProps> = ({
if (!mediaPlayer) return;
if (playing && !isPlayerBuffering) {
- mediaPlayer.play();
+ mediaPlayer.play(currentTimeRef.current);
} else {
mediaPlayer.pause();
}