diff --git a/src/components/canvas/players/video-player.ts b/src/components/canvas/players/video-player.ts index c21900ba..5baab7ef 100644 --- a/src/components/canvas/players/video-player.ts +++ b/src/components/canvas/players/video-player.ts @@ -129,10 +129,10 @@ export class VideoPlayer extends Player { const desyncThreshold = 0.3; if (shouldClipPlay) { + this.applyPlaybackVolume(); if (!this.isPlaying) { this.isPlaying = true; this.activeSyncTimer = 0; - this.texture.source.resource.volume = this.getVolume(); // Preserve the prepared frame unless playback has moved substantially. if (Math.abs(this.texture.source.resource.currentTime - sourceTime) > desyncThreshold) { this.texture.source.resource.currentTime = sourceTime; @@ -140,10 +140,6 @@ export class VideoPlayer extends Player { this.texture.source.resource.play().catch(console.error); } - if (this.texture.source.resource.volume !== this.getVolume()) { - this.texture.source.resource.volume = this.getVolume(); - } - if (this.texture.source.resource.playbackRate !== speed) { this.texture.source.resource.playbackRate = speed; } @@ -257,7 +253,7 @@ export class VideoPlayer extends Player { } const loadUrl = toLoadUrl(src); - const loadOptions: pixi.UnresolvedAsset = { src: loadUrl, data: { autoPlay: false, muted: false } }; + const loadOptions: pixi.UnresolvedAsset = { src: loadUrl, data: { autoPlay: false, muted: videoAsset.volume === 0 } }; // Use unique loader to create independent video element per player // This prevents conflicts when multiple clips use the same video source @@ -298,10 +294,8 @@ export class VideoPlayer extends Player { this.sprite = new pixi.Sprite(this.texture); this.contentContainer.addChild(this.sprite); - // Set initial volume immediately so the element never sits at the browser default of 1.0 - this.texture.source.resource.volume = this.getVolume(); - - this.texture.source.resource.preservesPitch = false; + this.applyPlaybackVolume(); + video.preservesPitch = false; } private disposeVideo(): void { @@ -337,6 +331,19 @@ export class VideoPlayer extends Player { return this.volumeKeyframeBuilder.getValue(this.getPlaybackTime()); } + private applyPlaybackVolume(): void { + const video = this.texture?.source.resource; + if (!video) return; + + // iOS can ignore volume writes, so a clip configured at zero needs an explicit mute. + const muted = (this.clipConfiguration.asset as VideoAsset).volume === 0; + if (!muted) { + const volume = this.getVolume(); + if (video.volume !== volume) video.volume = volume; + } + if (video.muted !== muted) video.muted = muted; + } + public override getAssetSpeed(): number { return this.animatedSpeed ? this.animatedSpeed.curve.getValue(this.getPlaybackTime()) : super.getAssetSpeed(); } diff --git a/tests/media-player-fallback.test.ts b/tests/media-player-fallback.test.ts index c2245ea1..ba875481 100644 --- a/tests/media-player-fallback.test.ts +++ b/tests/media-player-fallback.test.ts @@ -220,6 +220,40 @@ function createVideoTexture(width: number, height: number) { } describe("media player fallbacks", () => { + it("mutes zero-volume clips even when the browser ignores volume writes", async () => { + const edit = createEdit(); + const video = document.createElement("video"); + const setVolume = jest.fn(); + const play = jest.fn(() => Promise.resolve(video.muted)); + Object.defineProperties(video, { + readyState: { value: 4 }, + volume: { get: () => 1, set: setVolume }, + play: { value: play } + }); + const texture = new pixi.Texture({ source: new pixi.VideoSource({ resource: video }) }); + edit.assetLoader.loadVideoUnique.mockResolvedValueOnce(texture); + const asset = { type: "video" as const, src: "https://example.com/video.mp4", volume: 0 }; + const player = new VideoPlayer(edit as never, { ...createVideoClip(), asset }); + await player.load(); + + expect(edit.assetLoader.loadVideoUnique).toHaveBeenCalledWith(expect.any(String), { + src: expect.any(String), + data: { autoPlay: false, muted: true } + }); + expect(video.muted).toBe(true); + edit.isPlaying = true; + player.update(0, 16); + expect(play).toHaveBeenCalledTimes(1); + await expect(play.mock.results[0].value).resolves.toBe(true); + expect(setVolume).not.toHaveBeenCalled(); + + asset.volume = 0.35; + player.reconfigureAfterRestore(); + player.update(0, 16); + expect(video.muted).toBe(false); + expect(setVolume).toHaveBeenLastCalledWith(0.35); + }); + it.each([1, 1.5])("prepares trimmed frames before display and after rewind at speed %s", async speed => { const edit = createEdit(); edit.playbackTime = 12.47;