Skip to content

🎥 Make videos programmatically with React

fix audio seekings

Jonny Burgerea6f422fdec9
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index a5a9ca128f..cbccd64a17 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -28,6 +28,7 @@ export const audioIteratorManager = ({
getStartTime,
initialMuted,
drawDebugOverlay,
+ initialTime,
}: {
audioTrack: InputAudioTrack;
delayPlaybackHandleIfNotPremounting: () => DelayPlaybackIfNotPremounting;
@@ -37,9 +38,11 @@ export const audioIteratorManager = ({
getStartTime: () => number;
initialMuted: boolean;
drawDebugOverlay: () => void;
+ initialTime: number;
}) => {
let muted = initialMuted;
let currentVolume = 1;
+ let currentSeek = initialTime;
const gainNode = sharedAudioContext.audioContext.createGain();
gainNode.connect(sharedAudioContext.audioContext.destination);
@@ -369,6 +372,12 @@ export const audioIteratorManager = ({
getAudioContextState: () => AudioContextState;
getAudioContextOutputTimestamp: () => number;
}) => {
+ if (currentSeek === newTime) {
+ return;
+ }
+
+ currentSeek = newTime;
+
if (muted) {
return;
}
@@ -416,14 +425,6 @@ export const audioIteratorManager = ({
}
}
- console.log(
- 'new iterator',
- Boolean(audioBufferIterator),
- audioBufferIterator?.guessNextTimestamp(),
- audioBufferIterator?.getQueuedPeriod(),
- audioBufferIterator?.isDestroyed(),
- newTime,
- );
await startAudioIterator({
nonce,
playbackRate,
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 0be5084c23..a157f3b991 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -311,6 +311,7 @@ export class MediaPlayer {
getStartTime: () => this.getStartTime(),
initialMuted,
drawDebugOverlay: this.drawDebugOverlay,
+ initialTime: startTime,
});
}
@@ -398,34 +399,26 @@ export class MediaPlayer {
return;
}
- const shouldSeekAudio =
- this.audioIteratorManager &&
- this.getAudioPlaybackTime(
- this.sharedAudioContext?.audioContext.currentTime ?? 0,
- ) !== newTime;
-
try {
await Promise.all([
this.videoIteratorManager?.seek({
newTime,
nonce,
}),
- shouldSeekAudio
- ? this.audioIteratorManager?.seek({
- newTime,
- nonce,
- playbackRate: this.playbackRate * this.globalPlaybackRate,
- getIsPlaying: () => this.playing,
- scheduleAudioNode: this.scheduleAudioNode,
- debugAudioScheduling: this.debugAudioScheduling,
- getTargetTime: this.getTargetTime,
- getAudioContextState: () =>
- this.sharedAudioContext?.audioContext.state ?? 'suspended',
- getAudioContextOutputTimestamp: () =>
- this.sharedAudioContext?.audioContext.getOutputTimestamp()
- .contextTime ?? 0,
- })
- : null,
+ this.audioIteratorManager?.seek({
+ newTime,
+ nonce,
+ playbackRate: this.playbackRate * this.globalPlaybackRate,
+ getIsPlaying: () => this.playing,
+ scheduleAudioNode: this.scheduleAudioNode,
+ debugAudioScheduling: this.debugAudioScheduling,
+ getTargetTime: this.getTargetTime,
+ getAudioContextState: () =>
+ this.sharedAudioContext?.audioContext.state ?? 'suspended',
+ getAudioContextOutputTimestamp: () =>
+ this.sharedAudioContext?.audioContext.getOutputTimestamp()
+ .contextTime ?? 0,
+ }),
]);
} catch (error) {
if (this.isDisposalError()) {
@@ -694,26 +687,6 @@ export class MediaPlayer {
});
};
- private getAudioPlaybackTime(currentTime: number): number {
- if (!this.sharedAudioContext) {
- throw new Error('Shared audio context not found');
- }
-
- const globalTime =
- (currentTime - this.sharedAudioContext.audioSyncAnchor.value) *
- this.globalPlaybackRate;
- const localTime = globalTime - this.sequenceOffset;
-
- // Pass through getTrimmedTime to apply loop wrapping and trim
- const trimmedTime = this.getTrimmedTime(localTime);
- if (trimmedTime !== null) {
- return trimmedTime;
- }
-
- // Fallback for when time is outside valid range
- return localTime * this.playbackRate + (this.trimBefore ?? 0) / this.fps;
- }
-
public setVideoFrameCallback(
callback: null | ((frame: CanvasImageSource) => void),
) {
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index 03de2fc814..ccf7ab6315 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -40,6 +40,7 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
getStartTime: () => 0,
initialMuted: false,
drawDebugOverlay: () => {},
+ initialTime: 0,
});
const getIsPlaying = () => true;
@@ -265,6 +266,7 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
getStartTime: () => 0,
initialMuted: false,
drawDebugOverlay: () => {},
+ initialTime: 0,
});
const scheduledChunks: {
diff --git a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
index cc4d098c8f..28716f1a35 100644
--- a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
+++ b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
@@ -102,6 +102,7 @@ test('same goes for audio', async () => {
getStartTime: () => 0,
initialMuted: false,
drawDebugOverlay: () => {},
+ initialTime: 0,
});
const nonceManager = makeNonceManager();