@devries-cameron20 / t3code-1 / commits / c079503d07
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): release iOS audio session after video playback (#18539)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 15, 2026 at 13:00 UTC c079503d07739096e9e672bfa42fa0fe42a8ae15
7 files changed +170 -8
apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift
+13
@@ -1,3 +1,4 @@
1 +import AVFoundation
2 import ExpoModulesCore
3 import Security
4 import UIKit
@@ -97,6 +98,18 @@ public final class T3NativeControlsModule: Module {
98 dismissVideoPresentation(identifier: identifier)
99 }
100
101 + // expo-video activates the session for inline playback but never deactivates
102 + // it. Any other category or mode means another feature (recording) owns it.
103 + @JS
104 + @MainActor
105 + func releaseVideoAudioSession() async {
106 + // The fullscreen presentation releases the session in its own teardown.
107 + guard videoPresentation == nil else { return }
108 + let session = AVAudioSession.sharedInstance()
109 + guard session.category == .playback, session.mode == .moviePlayback else { return }
110 + try? session.setActive(false, options: [.notifyOthersOnDeactivation])
111 + }
112 +
113 @JS
114 @MainActor
115 func dismissFile(identifier: String) async {
apps/mobile/modules/t3-native-controls/ios/T3NativeVideoPresentation.swift
+7 -3
@@ -152,15 +152,19 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
152 if let backgroundObserver { NotificationCenter.default.removeObserver(backgroundObserver) }
153 backgroundObserver = nil
154 let audioSession = AVAudioSession.sharedInstance()
155 + // Still what present() set, allowing the .mixWithOthers flip expo-video
156 + // makes when the inline players pause under this preview. A recorder that
157 + // claimed the session since owns it.
158 if let previousAudioSession, audioSession.category == .playback,
156 - audioSession.mode == .moviePlayback, audioSession.categoryOptions.isEmpty {
157 - // AVPlayer owns activation. Deactivating the shared session here could
158 - // stop another player or recorder that was active before this preview.
159 + audioSession.mode == .moviePlayback,
160 + audioSession.categoryOptions.isSubset(of: [.mixWithOthers]) {
161 try? audioSession.setCategory(
162 previousAudioSession.category,
163 mode: previousAudioSession.mode,
164 options: previousAudioSession.options
165 )
166 + // Deactivating with notification lets interrupted apps resume.
167 + try? audioSession.setActive(false, options: [.notifyOthersOnDeactivation])
168 }
169 completion(playbackError)
170 }
apps/mobile/src/components/AudioFilePreview.tsx
+8 -1
@@ -1,7 +1,8 @@
1 import { useAudioPlayer, useAudioPlayerStatus } from "expo-audio";
2 -import { useState } from "react";
2 +import { useEffect, useState } from "react";
3 import { Pressable, View } from "react-native";
4 import { AppText as Text } from "./AppText";
5 +import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../lib/videoAudioSession";
6
7 function timestamp(seconds: number) {
8 const value = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0;
@@ -12,6 +13,12 @@ export function AudioFilePreview(props: { uri: string; onRetry: () => void }) {
13 const player = useAudioPlayer({ uri: props.uri }, { updateInterval: 500 });
14 const status = useAudioPlayerStatus(player);
15 const [seekError, setSeekError] = useState(false);
16 + // Video teardown deactivates the shared iOS session, which would stop this player.
17 + useMediaAudioSessionHold(status.playing);
18 + const fullscreenVideoOpen = useFullscreenVideoOpen();
19 + useEffect(() => {
20 + if (fullscreenVideoOpen) player.pause();
21 + }, [fullscreenVideoOpen, player]);
22 const seek = (seconds: number, play = false) => {
23 setSeekError(false);
24 void player
apps/mobile/src/components/MediaVideoPlayer.tsx
+9 -4
@@ -10,6 +10,7 @@ import { SymbolView } from "./AppSymbol";
10 import { VideoThumbnailImage } from "./VideoThumbnailImage";
11 import { useMediaActions } from "../state/mediaActions";
12 import { type MediaActionsSource } from "../lib/mediaActionsSource";
13 +import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../lib/videoAudioSession";
14 import { MediaActionsMenu } from "./MediaActionsMenu";
15
16 /** Loads only after Play or opening the viewer. Source replacement never starts playback itself. */
@@ -20,6 +21,8 @@ function LoadedMediaVideo(props: {
21 readonly paused: boolean;
22 }) {
23 const focused = useIsFocused();
24 + const fullscreenVideoOpen = useFullscreenVideoOpen();
25 + const paused = props.paused || fullscreenVideoOpen;
26 const active = useRef(focused && AppState.currentState === "active");
27 const fullscreen = useRef(false);
28 const [attempt, setAttempt] = useState(0);
@@ -30,6 +33,8 @@ function LoadedMediaVideo(props: {
33 player.bufferOptions = { preferredForwardBufferDuration: 5 };
34 });
35 const { status } = useEvent(player, "statusChange", { status: player.status });
36 + const { isPlaying } = useEvent(player, "playingChange", { isPlaying: player.playing });
37 + useMediaAudioSessionHold(isPlaying);
38 const loadSource = useEffectEvent(async (signal: AbortSignal) => {
39 const uri = props.resolvePlaybackUri ? await props.resolvePlaybackUri() : props.uri;
40 if (signal.aborted) return;
@@ -40,16 +45,16 @@ function LoadedMediaVideo(props: {
45 });
46
47 useEffect(() => {
43 - active.current = focused && !props.paused && AppState.currentState === "active";
44 - if (!focused || props.paused || (!active.current && !fullscreen.current)) player.pause();
48 + active.current = focused && !paused && AppState.currentState === "active";
49 + if (!focused || paused || (!active.current && !fullscreen.current)) player.pause();
50 // Native background handling distinguishes Android's fullscreen activity
51 // from leaving the app; React Native reports both as background.
52 const subscription = AppState.addEventListener("change", (state) => {
48 - active.current = focused && !props.paused && state === "active";
53 + active.current = focused && !paused && state === "active";
54 if (state === "inactive" || (state === "background" && !fullscreen.current)) player.pause();
55 });
56 return () => subscription.remove();
52 - }, [focused, player, props.paused]);
57 + }, [focused, player, paused]);
58
59 useEffect(() => {
60 const controller = new AbortController();
apps/mobile/src/components/VideoPreviewModal.ios.tsx
+5
@@ -4,6 +4,7 @@ import { useEffect, useEffectEvent, useId, useState } from "react";
4 import { Alert, Keyboard } from "react-native";
5
6 import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
7 +import { fullscreenVideoClosed, fullscreenVideoOpened } from "../lib/videoAudioSession";
8 import { mediaVideoPreviewUri, type VideoPreviewSource } from "../lib/videoPreviewSource";
9 import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
10 import { usePreparedConnection } from "../state/session";
@@ -51,6 +52,10 @@ function NativeVideoPreview(props: {
52 : null;
53
54 useEffect(() => Keyboard.dismiss(), []);
55 + useEffect(() => {
56 + fullscreenVideoOpened();
57 + return () => fullscreenVideoClosed();
58 + }, []);
59 useEffect(() => {
60 if (playbackUrl === null && resolvedUrl !== null) setPlaybackUrl(resolvedUrl);
61 }, [playbackUrl, resolvedUrl]);
apps/mobile/src/lib/videoAudioSession.test.ts new
+54
@@ -0,0 +1,54 @@
1 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2 +
3 +const mocks = vi.hoisted(() => ({
4 + platform: { OS: "ios" as "ios" | "android" },
5 + release: vi.fn(async (): Promise<void> => {}),
6 +}));
7 +vi.mock("react-native", () => ({ Platform: mocks.platform }));
8 +vi.mock("expo", () => ({
9 + requireOptionalNativeModule: () => ({ releaseVideoAudioSession: mocks.release }),
10 +}));
11 +
12 +let session: typeof import("./videoAudioSession");
13 +
14 +beforeEach(async () => {
15 + vi.resetModules();
16 + vi.useFakeTimers();
17 + mocks.platform.OS = "ios";
18 + mocks.release.mockClear().mockResolvedValue(undefined);
19 + session = await import("./videoAudioSession");
20 +});
21 +
22 +afterEach(() => vi.useRealTimers());
23 +
24 +describe("video audio session", () => {
25 + it("releases once, after the last playing player stops, unless playback restarts first", () => {
26 + session.mediaPlaybackStarted();
27 + session.mediaPlaybackStarted();
28 + session.mediaPlaybackStopped();
29 + vi.advanceTimersByTime(1_000);
30 + expect(mocks.release).not.toHaveBeenCalled();
31 +
32 + // A player starting inside the deferral window keeps the session.
33 + session.mediaPlaybackStopped();
34 + session.mediaPlaybackStarted();
35 + vi.advanceTimersByTime(1_000);
36 + expect(mocks.release).not.toHaveBeenCalled();
37 +
38 + session.mediaPlaybackStopped();
39 + vi.advanceTimersByTime(1_000);
40 + expect(mocks.release).toHaveBeenCalledTimes(1);
41 + });
42 +
43 + it("stays fullscreen until every overlapping presentation closes", () => {
44 + const listener = vi.fn();
45 + session.subscribeFullscreenVideo(listener);
46 + session.fullscreenVideoOpened();
47 + session.fullscreenVideoOpened();
48 + session.fullscreenVideoClosed();
49 + expect(session.isFullscreenVideoOpen()).toBe(true);
50 + session.fullscreenVideoClosed();
51 + expect(session.isFullscreenVideoOpen()).toBe(false);
52 + expect(listener).toHaveBeenCalledTimes(4);
53 + });
54 +});
apps/mobile/src/lib/videoAudioSession.ts new
+74
@@ -0,0 +1,74 @@
1 +import { requireOptionalNativeModule } from "expo";
2 +import { useEffect, useSyncExternalStore } from "react";
3 +import { Platform } from "react-native";
4 +
5 +const nativeControls = requireOptionalNativeModule<{
6 + releaseVideoAudioSession(): Promise<void>;
7 +}>("T3NativeControls");
8 +
9 +// Players report not playing during seeks and buffering stalls, and another
10 +// player may start right after one stops. Both keep the session.
11 +const RELEASE_DELAY_MS = 1_000;
12 +
13 +let playingCount = 0;
14 +let releaseTimer: ReturnType<typeof setTimeout> | undefined;
15 +
16 +/** Pairs with `mediaPlaybackStopped` for each inline video or audio player holding the session. */
17 +export function mediaPlaybackStarted(): void {
18 + playingCount += 1;
19 +}
20 +
21 +/**
22 + * expo-video activates the iOS playback session but never deactivates it, so
23 + * interrupted apps (Spotify, Podcasts) are never told they may resume. The
24 + * last player to stop releases it. In-app audio previews count too, since
25 + * deactivating stops them.
26 + */
27 +export function mediaPlaybackStopped(): void {
28 + playingCount -= 1;
29 + if (playingCount > 0 || Platform.OS !== "ios") return;
30 + clearTimeout(releaseTimer);
31 + releaseTimer = setTimeout(() => {
32 + if (playingCount > 0) return;
33 + void nativeControls?.releaseVideoAudioSession().catch(() => undefined);
34 + }, RELEASE_DELAY_MS);
35 +}
36 +
37 +let fullscreenCount = 0;
38 +const fullscreenListeners = new Set<() => void>();
39 +
40 +/** Pairs with `fullscreenVideoClosed` while the native fullscreen player is open. */
41 +export function fullscreenVideoOpened(): void {
42 + fullscreenCount += 1;
43 + for (const listener of fullscreenListeners) listener();
44 +}
45 +
46 +export function fullscreenVideoClosed(): void {
47 + fullscreenCount -= 1;
48 + for (const listener of fullscreenListeners) listener();
49 +}
50 +
51 +export function subscribeFullscreenVideo(listener: () => void): () => void {
52 + fullscreenListeners.add(listener);
53 + return () => {
54 + fullscreenListeners.delete(listener);
55 + };
56 +}
57 +
58 +export function isFullscreenVideoOpen(): boolean {
59 + return fullscreenCount > 0;
60 +}
61 +
62 +/** In-app players pause while the iOS fullscreen player owns playback and the session. */
63 +export function useFullscreenVideoOpen(): boolean {
64 + return useSyncExternalStore(subscribeFullscreenVideo, isFullscreenVideoOpen);
65 +}
66 +
67 +/** Holds the session for as long as `active`, releasing it with the last holder. */
68 +export function useMediaAudioSessionHold(active: boolean): void {
69 + useEffect(() => {
70 + if (!active) return;
71 + mediaPlaybackStarted();
72 + return () => mediaPlaybackStopped();
73 + }, [active]);
74 +}