Skip to content

Commit c079503

Browse files
fix(mobile): release iOS audio session after video playback (#18539)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 26fffe5 commit c079503

7 files changed

Lines changed: 170 additions & 8 deletions

File tree

‎apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import AVFoundation
12
import ExpoModulesCore
23
import Security
34
import UIKit
@@ -97,6 +98,18 @@ public final class T3NativeControlsModule: Module {
9798
dismissVideoPresentation(identifier: identifier)
9899
}
99100

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+
100113
@JS
101114
@MainActor
102115
func dismissFile(identifier: String) async {

‎apps/mobile/modules/t3-native-controls/ios/T3NativeVideoPresentation.swift‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,19 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
152152
if let backgroundObserver { NotificationCenter.default.removeObserver(backgroundObserver) }
153153
backgroundObserver = nil
154154
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.
155158
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]) {
159161
try? audioSession.setCategory(
160162
previousAudioSession.category,
161163
mode: previousAudioSession.mode,
162164
options: previousAudioSession.options
163165
)
166+
// Deactivating with notification lets interrupted apps resume.
167+
try? audioSession.setActive(false, options: [.notifyOthersOnDeactivation])
164168
}
165169
completion(playbackError)
166170
}

‎apps/mobile/src/components/AudioFilePreview.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useAudioPlayer, useAudioPlayerStatus } from "expo-audio";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33
import { Pressable, View } from "react-native";
44
import { AppText as Text } from "./AppText";
5+
import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../lib/videoAudioSession";
56

67
function timestamp(seconds: number) {
78
const value = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0;
@@ -12,6 +13,12 @@ export function AudioFilePreview(props: { uri: string; onRetry: () => void }) {
1213
const player = useAudioPlayer({ uri: props.uri }, { updateInterval: 500 });
1314
const status = useAudioPlayerStatus(player);
1415
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]);
1522
const seek = (seconds: number, play = false) => {
1623
setSeekError(false);
1724
void player

‎apps/mobile/src/components/MediaVideoPlayer.tsx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SymbolView } from "./AppSymbol";
1010
import { VideoThumbnailImage } from "./VideoThumbnailImage";
1111
import { useMediaActions } from "../state/mediaActions";
1212
import { type MediaActionsSource } from "../lib/mediaActionsSource";
13+
import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../lib/videoAudioSession";
1314
import { MediaActionsMenu } from "./MediaActionsMenu";
1415

1516
/** Loads only after Play or opening the viewer. Source replacement never starts playback itself. */
@@ -20,6 +21,8 @@ function LoadedMediaVideo(props: {
2021
readonly paused: boolean;
2122
}) {
2223
const focused = useIsFocused();
24+
const fullscreenVideoOpen = useFullscreenVideoOpen();
25+
const paused = props.paused || fullscreenVideoOpen;
2326
const active = useRef(focused && AppState.currentState === "active");
2427
const fullscreen = useRef(false);
2528
const [attempt, setAttempt] = useState(0);
@@ -30,6 +33,8 @@ function LoadedMediaVideo(props: {
3033
player.bufferOptions = { preferredForwardBufferDuration: 5 };
3134
});
3235
const { status } = useEvent(player, "statusChange", { status: player.status });
36+
const { isPlaying } = useEvent(player, "playingChange", { isPlaying: player.playing });
37+
useMediaAudioSessionHold(isPlaying);
3338
const loadSource = useEffectEvent(async (signal: AbortSignal) => {
3439
const uri = props.resolvePlaybackUri ? await props.resolvePlaybackUri() : props.uri;
3540
if (signal.aborted) return;
@@ -40,16 +45,16 @@ function LoadedMediaVideo(props: {
4045
});
4146

4247
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();
4550
// Native background handling distinguishes Android's fullscreen activity
4651
// from leaving the app; React Native reports both as background.
4752
const subscription = AppState.addEventListener("change", (state) => {
48-
active.current = focused && !props.paused && state === "active";
53+
active.current = focused && !paused && state === "active";
4954
if (state === "inactive" || (state === "background" && !fullscreen.current)) player.pause();
5055
});
5156
return () => subscription.remove();
52-
}, [focused, player, props.paused]);
57+
}, [focused, player, paused]);
5358

5459
useEffect(() => {
5560
const controller = new AbortController();

‎apps/mobile/src/components/VideoPreviewModal.ios.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useEffect, useEffectEvent, useId, useState } from "react";
44
import { Alert, Keyboard } from "react-native";
55

66
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
7+
import { fullscreenVideoClosed, fullscreenVideoOpened } from "../lib/videoAudioSession";
78
import { mediaVideoPreviewUri, type VideoPreviewSource } from "../lib/videoPreviewSource";
89
import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
910
import { usePreparedConnection } from "../state/session";
@@ -51,6 +52,10 @@ function NativeVideoPreview(props: {
5152
: null;
5253

5354
useEffect(() => Keyboard.dismiss(), []);
55+
useEffect(() => {
56+
fullscreenVideoOpened();
57+
return () => fullscreenVideoClosed();
58+
}, []);
5459
useEffect(() => {
5560
if (playbackUrl === null && resolvedUrl !== null) setPlaybackUrl(resolvedUrl);
5661
}, [playbackUrl, resolvedUrl]);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)